Question

I want to accomplish a loop over R code within an IPython notebook. What is the best way to do this?

    l = []
    for i in range(10):
        # execute R script
        %%R -i i -o result   #some arbitrary R code
        # and use the output
        l.append(result)

Can this be done inside a notebook (Looping over next cell)?

Was it helpful?

Solution

Have you looked into rmagic and rpy2 module?

If you have R scripts, then you can call them and assign their output to a variable using the shell command notation:

     var=!R_script arguments....

The above does not need you need you to install rpy2 since ! shell command execution is basic in ipython. You can pass values of variables from ipython notebook by using $var in the arg list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top