1

the screenshot for jupyternotebook

I've successfully installed ryp2 using wheel. I'm using Python 3.7.6 and rpy2 2.9.5. I can get the output from jupter notebook when running python code but it doesn't work when using R magic. I tried to see if I can really use R by installing 'ggplot2' and it worked with installation bar poped out.

However, I cannot get any output when I use R magic.

Errors will come out like this. The error is so weird Error in withVisible({ : object 'returns' not found. returns is a python dataframe and was not converted to r object. Error in withVisible({

Other error like below. Error in eval(predvars, data, env) : object 'p' not found. Neither p nor q is initialized. Error in eval(predvars, data, env)

Anyone encountered the same problem? Thank you very much in advance!

FYI: packages version

  • 1
    Please copy-paste the text from screenshots into the body of your question. You are missing out on answers from users like me for whom reading from screenshots may be difficult. – krassowski Jun 18 '21 at 08:31
  • Thank you for your advice! But I'm new to stack overflow and I don't have enough reputations to insert the image to the body. – asmallpotato Jun 18 '21 at 08:39
  • No worries, no need to embed pictures (links are fine). It is just that in addition to having the picture, copy-pasting a reproducible example helps a lot for users with vision deficiency, screen readers and majority of users with healthy vision as well. – krassowski Jun 18 '21 at 08:41

1 Answers1

1

This is only a partial answer; I do not know why you cannot see output of 1 for:

%%R
a = 1
a

but I know that the "errors" you see are there because the variables are indeed not defined - you still need to tell rpy2 to import (-i for input) or export (-o for output) variables between R and Python namespaces, this does not happen automatically (if it did you would end up with each object taking up twice as much space!). This is done using:

%R -i variable_to_import_from_python

And

%R -o variable_to_export_to_python

(or equivalently using %%R). It means that in your examples in order to Python print(a) you first need to:

%%R -o a
a = 1
a

And to have Python variable b, say b = 1 in R you would need to:

%%R -i b
cat(b)

Please read the documentation for using rpy2 in notebooks (magics are covered near the end) and the documentation of RMagic, as it is already explained in there.

I hope you will get an answer on the non-functioning R output soon. If you do not get one in the next few days please consider opening a bug report on rpy2 GitHub.

krassowski
  • 13,598
  • 4
  • 60
  • 92
  • Thank you for your comment! May I know do you know why the error contains 'withVisible' ? I know as I didn't introduce my object to r but I don't know why the error contains `withVisible{`. – asmallpotato Jun 18 '21 at 08:50
  • Because this is how rpy2 magic [evaluates your code](https://github.com/rpy2/rpy2/blob/74e753cef9df37903cfa771a60bcf8cf89cc9d1e/rpy2/ipython/rmagic.py#L252-L282); you can safely ignore it. Maybe it will get prettier in future versions. – krassowski Jun 18 '21 at 08:55