6

I am trying to run R in a google collab notebook (which worked fine before, however as I now tried to access it and run it it keeps giving me an error).

I have ran the following:



    %load_ext rpy2.ipython

    %%R 
    install.packages('rworldmap')
    install.packages('classInt')
    install.packages('reshape2')
    install.packages('dplyr')
    install.packages('ggpubr')

NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'str'>'

I have tried re-opening the notebook or creating a new one but I keep having the same issue.

2 Answers 2

14

This is a problem with newer versions. You must downgrade rpy: run in a cell:

!pip install rpy2==3.5.1

restart and try again

From this post.

2
  • Hi, +1 on your answer. Thank you. Related question, do you happen to know why, when using %%R on a Colab document options(repr.plot.width=30, repr.plot.height=2) does not work, and what to do to change the size of the plot in R? Dec 7, 2022 at 18:15
  • I posted the question here. Dec 7, 2022 at 18:26
0

It would be best if you first uninstalled the currently loaded rpy2 package:

!pip uninstall rpy2 -y

Then install an older version of rpy2 (3.0.0, works for me):

!pip install rpy2==3.0.0

Use magic commands and enjoy R&python interactively:

%load_ext rpy2.ipython

Example:

%%R
x<-1:5
x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.