 CoCalc Public Filessupport / 2020-03-29-r-output.sagews
Author: William A. Stein
Description: Jupyter notebook support/2015-06-04-141749-bokeh.ipynb
x = sage.all.r('c(1,2,3)')
type(x)
v = x.sage(); print(v)
print(type(v))

<class 'sage.interfaces.r.RElement'> [1, 2, 3] <type 'list'>
# Also get a string if you want (no copy/paste)
str(x)

' 1 2 3'
# The global variable "r" in a Sage worksheet is just a Jupyter kernel, and it has
# no useful "magic" regarding interfacing with the sage session. Use sage.all.r,
# which is a pexpect interface to R and is very powerful for this.  Alternatively,
# if your data is HUGE, then use rpy2.
r??

   File: /cocalc/lib/python2.7/site-packages/smc_sagews/sage_salvus.py
Source:
def r(code=None, **kwargs):
r"""
Run R code in a sage worksheet.

INPUT:

- code -- a string containing code

Use as a decorator. For example, put this in a cell and evaluate it to see a scatter plot
of built-in mtcars dataframe variables mpg vs wt::

%r
with(mtcars,plot(wt,mpg))

.. note::

SMC %r mode uses the jupyter ir kernel.
"""
if r.jupyter_kernel is None:
r.jupyter_kernel = jupyter("ir")
r.jupyter_kernel('options(repr.plot.res = 240)')
r.jupyter_kernel.smc_image_scaling = .5
return r.jupyter_kernel(code, **kwargs)


%sage.all.r
v <- c(1,2,3)

 1 2 3
sage.all.r('v').sage()

[1, 2, 3]