Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 369
jodoox Avatar asked Jul 18 '16 14:07

jodoox


People also ask

Why the output is not showing in Jupyter Notebook?

It is your expection that needs to be adapted. The Jupyter Notebook only shows what the last statement of a cell evaluates to. If you define a function, this does not evaluate to anything that could be displayed. So you can't see anything in the upper cell.

How do I use Jupyter in command prompt?

Windows File Explorer + Command Prompt Once you've entered your specific folder with Windows Explorer, you can simply press ALT + D, type in cmd and press Enter. You can then type jupyter notebook to launch Jupyter Notebook within that specific folder.

How do I get output in Jupyter?

Jupyter Notebook can print the output of each cell just below the cell. When you have a lot of output you can reduce the amount of space it takes up by clicking on the left side panel of the output. This will turn the output into a scrolling window.


1 Answers

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.

like image 145
Mihai Chelaru Avatar answered Nov 06 '22 13:11

Mihai Chelaru