ipython notebook R cell magic as a python function

jwillis0720

I have some %rmagic that takes in a dataframe and plots it. I can't do this with the current ggplot implementation in python. So I was wondering if I could use a function to pass the dataframe to a cell that contains R code and possibly return it. Here is what I had in mind:

 #cell 1
 def plot_in_r(dataframe):
     pass_to_cell_2(dataframe)


 #cell 2
 %%R -i dataframe 
 ggplot(dataframe) + geom_bar()

It doesn't have to be a cell to other cell, but I need to pass something to R and have it plot inside the notebook through a function that I can use repeatedly.

cel

I guess the simplest way to do that is to define a function in R which takes a data frame and contains the plotting logic.

necessariy imports:

import rpy2
import pandas as pd
%load_ext rpy2.ipython
%R require("ggplot2")

creating dummy data frames:

df1 = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]})
df2 = pd.DataFrame({"A": [3, 2, 1], "B": [1, 2, 3]})

writing the plotting function:

%%R
plot_gg <- function(df) {
    p <- ggplot(data=df) + geom_line(aes(x=A, y=B))
    print(p)
}

and finally plot with only two lines of code:

for df in df1, df2:
    %Rpush df
    %R plot_gg(df)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Execute IPython notebook cell from python script

From Dev

IPython notebook: How to write cell magic which can access notebook variables?

From Dev

IPython notebook: How to write cell magic which can access notebook variables?

From Dev

Bash cell magic in IPython script

From Dev

Ignore IPython magic in python

From Dev

IPython Notebook Magic Functions with environment variables

From Dev

ipython notebook background colour of a cell

From Java

IPython Notebook cell multiple outputs

From Dev

Read cell content in an ipython notebook

From Dev

IPython Notebook previous cell content

From Dev

What is the pure Python equivalent to the IPython magic function call %matplotlib inline?

From Dev

Capture the result of an IPython magic function

From Dev

iPython autoreload magic function not found

From Dev

Cython in Ipython: ERROR: Cell magic `%%cython` not found

From Dev

ipython %%javascript magic output into cell and console

From Dev

IPython Cell Magic to Run this Cell in Client and in All Engines

From Dev

IPython Cell Magic to Run this Cell in Client and in All Engines

From Dev

ipython notebook align table to the left of cell

From Java

Display multiple images in one IPython Notebook cell?

From Dev

Export individual cell in IPython/Jupyter notebook

From Dev

How to reference a IPython notebook cell in markdown?

From Dev

ipython notebook clear cell output in code

From Dev

ipython notebook read multiple images and display in CELL

From Dev

Programmatically get current Ipython notebook cell output?

From Dev

Jump to next code cell (IPython notebook)

From Dev

IPython Notebook - early exit from cell

From Dev

Javascript to scroll an iPython Notebook cell to the top of the window

From Dev

ipython notebook read multiple images and display in CELL

From Dev

Ipython notebook: generate log output in one cell; move to specific line in that cells output based on actions of function call in subsequent cell?

Related Related

  1. 1

    Execute IPython notebook cell from python script

  2. 2

    IPython notebook: How to write cell magic which can access notebook variables?

  3. 3

    IPython notebook: How to write cell magic which can access notebook variables?

  4. 4

    Bash cell magic in IPython script

  5. 5

    Ignore IPython magic in python

  6. 6

    IPython Notebook Magic Functions with environment variables

  7. 7

    ipython notebook background colour of a cell

  8. 8

    IPython Notebook cell multiple outputs

  9. 9

    Read cell content in an ipython notebook

  10. 10

    IPython Notebook previous cell content

  11. 11

    What is the pure Python equivalent to the IPython magic function call %matplotlib inline?

  12. 12

    Capture the result of an IPython magic function

  13. 13

    iPython autoreload magic function not found

  14. 14

    Cython in Ipython: ERROR: Cell magic `%%cython` not found

  15. 15

    ipython %%javascript magic output into cell and console

  16. 16

    IPython Cell Magic to Run this Cell in Client and in All Engines

  17. 17

    IPython Cell Magic to Run this Cell in Client and in All Engines

  18. 18

    ipython notebook align table to the left of cell

  19. 19

    Display multiple images in one IPython Notebook cell?

  20. 20

    Export individual cell in IPython/Jupyter notebook

  21. 21

    How to reference a IPython notebook cell in markdown?

  22. 22

    ipython notebook clear cell output in code

  23. 23

    ipython notebook read multiple images and display in CELL

  24. 24

    Programmatically get current Ipython notebook cell output?

  25. 25

    Jump to next code cell (IPython notebook)

  26. 26

    IPython Notebook - early exit from cell

  27. 27

    Javascript to scroll an iPython Notebook cell to the top of the window

  28. 28

    ipython notebook read multiple images and display in CELL

  29. 29

    Ipython notebook: generate log output in one cell; move to specific line in that cells output based on actions of function call in subsequent cell?

HotTag

Archive