Using rpy2 in jupyter notebook but there is no output. Error in withVisible({ : object 'returns' not found

405 Views Asked by At

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

There are 1 best solutions below

2
krassowski On

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.