jodoox
jodoox

Reputation: 829

Jupyter + rpy2 outputs to command prompt instead of notebook cell

Issue description

Whenever loading rpy2 in a Jupyter notebook, R code executed with a %%Rcell magic gets printed in the command prompt instead of the notebook cell. Plots show up properly in the notebook though.

enter image description here

I also noticed that the %R inline magic works properly, with code printed in the cell as expected.

Installation steps

  1. Python 2.7.11 :: Anaconda 2.5.0 (64-bit)
  2. notebook 4.1.0 installed via pip
  3. R 3.3.1 (x64) located in Program Files
  4. rpy2-2.7.8-cp27-none-win_amd64.whl installed via pip
  5. PATH:
    • R_USER = C:\Users\myusername
    • R_HOME = C:\Program Files\R\R-3.3.1
    • Added C:\Program Files\R\R-3.3.1\bin\i386

Upvotes: 9

Views: 1251

Answers (1)

Mihai Chelaru
Mihai Chelaru

Reputation: 8242

You can use the RWinOut Jupyter extension. The following installation instructions are taken from the GitHub page:

You can run the following curl command from a Jupyter notebook cell to download the file to your working directory. You can also download it manually and put it there yourself.

!curl -O "https://raw.githubusercontent.com/vitorcurtis/RWinOut/master/RWinOut.py"

Once it's in your working directory, you can replace %load_ext rpy2.ipython at the top of your script with %load_ext RWinOut. Then you should be able to see the output of cells containing the %%R magic as normal.

This isn't a complete solution, but it's a workaround that might achieve the same effect. I'm not sure if this breaks some functionality, but it seems to work fine for me.

Tested with:

  • Windows 10 v.1809 Build 17763.503
  • Python 3.7.2
  • R 3.6.0
  • rpy2 2.9.5

One other option is to use the Windows Subsystem for Linux and launch your Jupyter notebook from there. This might not be desirable if you have to reinstall a lot of R and Python packages into a different environment, but will make it so the output will print correctly without requiring this hacky workaround.

Upvotes: 2

Related Questions