Skip to content
Advertisement

%% Cell magic tag not working in Jupyter notebook?

I’m new to Jupyter notebook and I’m trying to set one up with Python and R, using rpy2. I have the line

%%R -i df

which gives me the error SyntaxError: invalid syntax

However when I use just one %, such as

%R require(ggplot2)

this works fine. How can I fix this issue? I am using Python 2.7.

Advertisement

Answer

% prefix is for a line magic, whereas %% prefix is for a cell magic.

%%R    # <-- must be the only instruction on this line
{body of cell in R code}

whereas:

%R {one line of R code}

I don’t have R installed, but I think you may have wanted to call a bash command on R; in that case, use ! to call the command:

!R -i df

for instance, if I type !python -i, I get info about my current python environment:

Python 3.6.2 |Anaconda custom (x86_64)| (default, Jul 20 2017, 13:14:59) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
Advertisement