I am trying to run a simple example found here: https://www.datacamp.com/community/blog/jupyter-notebook-r#gs.OczVCjA
import warnings
warnings.filterwarnings('ignore')
# Load in the r magic
import rpy2.ipython
%reload_ext rpy2.ipython
# We need ggplot2
%R require(ggplot2)
%R library("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))
At first I had a NameError "ggplot node defined"
I then added %R to the last line and now obtain the following output:
R object with classes: ('gg', 'ggplot') mapped to:
<ListVector - Python:0x7fc6a73c1d88 / R:0x3c4e768>
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...]
R object with classes: ('gg', 'ggplot') mapped to:
<ListVector - Python:0x7fc6a73c1d88 / R:0x3c4e768>
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...]
R object with classes: ('gg', 'ggplot') mapped to:
<ListVector - Python:0x7fc6a73c1d88 / R:0x3c4e768>
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...]
scales: <class 'rpy2.robjects.environments.Environment'>
R object with classes: ('ScalesList', 'ggproto') mapped to:
<Environment - Python:0x7fc6a7682808 / R:0x215a968>
...
data: <class 'rpy2.robjects.environments.Environment'>
R object with classes: ('FacetNull', 'Facet', 'ggproto') mapped to:
<Environment - Python:0x7fc6a76829c8 / R:0x281c138>
layers: <class 'rpy2.robjects.environments.Environment'>
R object with classes: ('environment',) mapped to:
<Environment - Python:0x7fc6a7682548 / R:0x1544d58>
R object with classes: ('gg', 'ggplot') mapped to:
<ListVector - Python:0x7fc6a73c1d88 / R:0x3c4e768>
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...]
Was a plot created and how to display it inline in the notebook like one would do with matplotlib?
n.b.: I installed R within Jupyter using the command conda install -c r r-essentials (wich normally includes ggplot?) as described in the link above
Thank you very much in advance
In this chapter, we will study how to do inline plotting with the Jupyter Notebook. In order to display the plot inside the notebook, you need to initiate plotly’s notebook mode as follows − Keep rest of the script as it is and run the notebook cell by pressing Shift+Enter. Graph will be displayed offline inside the notebook itself.
If I select the whole script with Ctrl+A, then Run the current line or selection ( Ctrl+Enter ), then the plot does display. Likewise, typing plotting commands into the console produces correct output. library (ggplot2) p = ggplot (mtcars, aes (wt, mpg)) p + geom_point () Will only produce output if pasted into console, not if sourced.
To use R in jupyter notebook click on R language and press open with jupyter. To create a new notebook for the R language, in the Jupyter Notebook menu, select New, then select R. To run the code, in the menu bar, click Cell then select Run Cells, or use the keyboard shortcut Ctrl-Enter.
In order to display the plot inside the notebook, you need to initiate plotly’s notebook mode as follows − Keep rest of the script as it is and run the notebook cell by pressing Shift+Enter. Graph will be displayed offline inside the notebook itself. The plot output shows a tool bar at top right.
Ok, i tried it now... This worked for me:
%R print(ggplot(data=df) + geom_point(aes(x=A, y=B, color=C)))
You have to add a print()
statement. Don't know why.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With