Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ipython magics in R jupyter notebook?

I installed jupyter with conda install jupyter and am running a notebook with the r kernal installed from conda create -n my-r-env -c r r-essentials

I am running a notebook and want to run a bash command from a shell.

!echo "hi"
Error in parse(text = x, srcfile = src): <text>:1:7: unexpected string constant
1: !echo "hi"

For comparison, in an notebook with a python kernel:

!echo "hi"
hi

Is there a way to set up R notebooks to have the same functionality as the ipython notebook with regards to bash commands (and maybe other magics)?

like image 788
elsherbini Avatar asked Feb 09 '16 17:02

elsherbini


People also ask

Can you use R and Python in the same Jupyter Notebook?

How to program with Python and R in the same Jupyter notebook. Python and R save their variables in different data types. So, if we want to use both languages together we will need a library that can “translate” Python lists to R vectors, for example.

How do I import IPython into Jupyter Notebook?

No more typing “import pandas as pd” 10 times a dayCreate a folder called startup if it's not already there. Add a new Python file called start.py. Put your favorite imports in this file. Launch IPython or a Jupyter Notebook and your favorite libraries will be automatically loaded every time!


2 Answers

For just bash commands, it's possible to get system commands to work. For example, in the IRkernel:

system("echo 'hi'", intern=TRUE)

Output:

'hi'

Or to see the first 5 lines of a file:

system("head -5 data/train.csv", intern=TRUE)

As IPython magics are available in the IPython kernel (but not in the IRkernel), I did a quick check if it was possible to access these using the rPython and PythonInR libraries. However, the issue is that get_ipython() isn't visible to the Python code, so none of the following worked:

library("rPython")
rPython::python.exec("from IPython import get_ipython; get_ipython().run_cell_magic('writefile', 'test.txt', 'This is a test')")

library("PythonInR")
PythonInR::pyExec("from IPython import get_ipython; get_ipython().run_cell_magic('head -5 data/test.csv')")
like image 78
nathanielng Avatar answered Oct 11 '22 07:10

nathanielng


A cosmetic improvement to the output of system can be obtained by wrapping the call in cat and specify '\n' as the separator, which displays the output on separate lines instead of separated by whitespaces which is the default for character vectors. This is very useful for commands like tree, since the format of the output makes little sense unless separated by newlines.

Compare the following for a sample directory named test:

Bash

$ tree test
test
├── A1.tif
├── A2.tif
├── A3.tif
├── README
└── src
    ├── sample.R
    └── sample2.R

1 directory, 6 files

R in Jupyter Notebook

system only, difficult to comprehend output:

> system('tree test', intern=TRUE)

'test' '├── A1.tif' '├── A2.tif' '├── A3.tif' '├── README' '└── src' '    ├── sample.R' '    └── sample2.R' '' '1 directory, 6 files

cat + system, output looks like in bash:

> cat(system('tree test', intern=TRUE), sep='\n')

test
├── A1.tif
├── A2.tif
├── A3.tif
├── README
└── src
    ├── sample.R
    └── sample2.R

1 directory, 6 files

Note that for commands like ls, the above would introduce a newline where outputs are normally separated by a space in bash.

You can create a function to save you some typing:

> # "jupyter shell" function
> js <- function(shell_command){
>     cat(system(shell_command, intern=TRUE), sep='\n')
> }
>
> # brief syntax for shell commands
> js('tree test')
like image 45
joelostblom Avatar answered Oct 11 '22 08:10

joelostblom