1

It might be a very simple question (although it seems long).

I installed Jupyter today from pip and pip3, so can set kernel as python2 or 3. Then started with an example taken from datacamp, it is:

# Hide warnings if there are any
import warnings
warnings.filterwarnings('ignore')

# Load in the r magic
%load_ext rpy2.ipython

# We need ggplot2
%R require(ggplot2)

# Load in the pandas library
import pandas as pd

# Make a pandas DataFrame
df = pd.DataFrame({'Alphabet': ['a', 'b', 'c', 'd','e', 'f', 'g', 'h','i'],
                   'A': [4, 3, 5, 2, 1, 7, 7, 5, 9],
                   'B': [0, 4, 3, 6, 7, 10, 11, 9, 13],
                   'C': [1, 2, 3, 1, 2, 3, 1, 2, 3]})

# Take the name of input variable df and assign it to an R variable of the same name
%%R -i df

# Plot the DataFrame df
ggplot(data=df) + geom_point(aes(x=A, y=B, color=C))

when press Ctrl+enter it returns:

File "<ipython-input-4-d85184fd21a2>", line 21
    %%R -i df
    ^
SyntaxError: invalid syntax

I have looked in the web but not sure what is the source of error. It might be something in my debian-stretch packages.

Example taken from https://www.datacamp.com/community/tutorials/tutorial-jupyter-notebook

Would you help me?

EDIT Code as proposed in one answer (by Vin) gives this output:

R object with classes: ('gg', 'ggplot') mapped to:
<ListVector - Python:0x7fe4b3a62e48 / R:0x5587100234b8>
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...]
R object with classes: ('gg', 'ggplot') mapped to:
<ListVector - Python:0x7fe4b3a62e48 / R:0x5587100234b8>
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...]
R object with classes: ('gg', 'ggplot') mapped to:
<ListVector - Python:0x7fe4b3a62e48 / R:0x5587100234b8>
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...]
  scales: <class 'rpy2.robjects.environments.Environment'>
  R object with classes: ('ScalesList', 'ggproto') mapped to:
<Environment - Python:0x7fe4b3a5fcc8 / R:0x55870fb79730>
  ...
  data: <class 'rpy2.robjects.environments.Environment'>
  R object with classes: ('FacetNull', 'Facet', 'ggproto') mapped to:
<Environment - Python:0x7fe4b3a7e588 / R:0x55870fb05b40>
  layers: <class 'rpy2.robjects.environments.Environment'>
  R object with classes: ('environment',) mapped to:
<Environment - Python:0x7fe4b3a5fcc8 / R:0x55870d7f2c48>
R object with classes: ('gg', 'ggplot') mapped to:
<ListVector - Python:0x7fe4b3a62e48 / R:0x5587100234b8>
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...]

But I suppose there should be some graph.

  • Of course it's not valid Python code. So... try to understand the example and fix it. – user202729 Jan 21 '18 at 06:31
  • I can't get what you say. %%R means r-programming language, and I suppose it is right as it is an example not an exercise..@user202729 –  Jan 21 '18 at 06:36
  • Is it Python or R? Looks like a mixture of the two languages. Are you sure you can mix them? – DYZ Jan 21 '18 at 06:38
  • Uh... what language is the code written in? Also why are there two `%`? – user202729 Jan 21 '18 at 06:38
  • Yes, in Jupyter, as far as I know, you can mix languages. Maybe the wrong thing is that I have all in one cell @DYZ –  Jan 21 '18 at 06:40
  • I think it is a mix. %%R is a magic command to specify that the following code is R-enconded @user202729 –  Jan 21 '18 at 06:41

1 Answers1

1

According to the second answer in this post, jupyter no longer allows you to define %%R at the middle or the end of the program. It must be at the very first line.

But if you try putting it in the first line, you won't be able to import pandas as anything typed below the cell will be considered R code. Thus I suggest you use just %R instead of %%R to be on the safer side.

As far as your code is concerned, here is a tweaked version that doesn't give any errors. Also I completely recoded it in R. Turns out due to the rule I mentioned above, you really can't use R cell bodies with Python code anymore.THis code works perfectly as far as the output is concerned:

%%R 
df <- data.frame(Alphabet=c('a', 'b', 'c', 'd','e', 'f', 'g', 'h','i'),
                 A=c(4, 3, 5, 2, 1, 7, 7, 5, 9),
                 B=c(0, 4, 3, 6, 7, 10, 11, 9, 13),
                 C=c(1, 2, 3, 1, 2, 3, 1, 2, 3))
library(ggplot2)
ggplot(data=df, aes(x=A, y=B,color=C)) +
geom_line()+
geom_point()
Vin
  • 729
  • 9
  • 15
  • I will try it in some minutes and accept if works, thanks! –  Jan 21 '18 at 09:17
  • Thanks for the reminder. No, it didn't.- Returns: `Error in withVisible({ couldn't find "ggplot" function` –  Jan 21 '18 at 10:44
  • I am installing ggplot but am not sure about how to do it –  Jan 21 '18 at 10:50
  • Check out the updated code. It works without any errors now. I guess you should consider sticking to just one language as of now in Jupyter – Vin Jan 21 '18 at 15:06
  • BTW sorry for the late response :) – Vin Jan 21 '18 at 15:22
  • I haven't seen the reminder! I can try it in some hours –  Jan 24 '18 at 11:58