Ask Your Question
1 isn't working anymore? (R Project)

asked 2017-12-08 18:07:43 +0200

Kristen Barth gravatar image

I am a beginner to Sage but I used to work with R. I know that I can create a R syntax cell by putting %r at the beginning of the cell. But there are a lot of examples which use the syntax (for example: x = r([10.4,5.6,3.1,6.4,21.7]); x). But none of these examples work for me, what am I doing wrong?

edit retag flag offensive close merge delete


We need much more information to understand your problem. Which version of Sage were you using before it failed? Was it Sage on your computer or online? Was it the Sage notebook or Jupyter?

vdelecroix gravatar imagevdelecroix ( 2017-12-08 22:04:39 +0200 )edit

+1 with Vincent. What did you type precisely to get an error ? On latest Sage version, i have:

sage: x = r([10.4,5.6,3.1,6.4,21.7]); 
sage: x
[1] 10.4  5.6  3.1  6.4 21.7
tmonteil gravatar imagetmonteil ( 2017-12-08 23:58:05 +0200 )edit

Thanks for reporting this bug. This bug is now tracked at .

slelievre gravatar imageslelievre ( 2017-12-10 20:08:29 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2017-12-09 19:07:39 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Hello, the r in CoCalc's Sage Worskheets is different from the r in Sage itself. The reason is that %r at the top of a cell launches the execution in an R jupyter kernel. Different philosophy altogether.

However, you can do r = R() in a sagews which creates and defines the same R-interpreter r.

r = R()

edit flag offensive delete link more


Thank you!

Kristen Barth gravatar imageKristen Barth ( 2017-12-09 21:20:04 +0200 )edit

answered 2017-12-09 07:45:44 +0200

slelievre gravatar image

updated 2022-07-07 17:57:02 +0200

When Sage starts, r is predefined as the R interpreter.

(Edit: this does not seem to be the case in CoCalc.)

In a new Sage session, with r predefined as above, the following works:

sage: r
R Interpreter
sage: x = r([10.4,5.6,3.1,6.4,21.7]); x
[1] 10.4  5.6  3.1  6.4 21.7

If in such a Sage session we assign some other value to r, then it stops working (of course).

sage: r = 2
sage: r
sage: x = r([10.4,5.6,3.1,6.4,21.7]); x
Traceback (most recent call last)
TypeError: 'sage.rings.integer.Integer' object is not callable

If we reset r, then it works again.

sage: reset('r')
sage: r
R Interpreter
sage: x = r([10.4,5.6,3.1,6.4,21.7]); x
[1] 10.4  5.6  3.1  6.4 21.7

To set r to be the R interpreter, whether the Sage being used predefines r that way or not, use:

sage: from sage.interfaces.r import r

In a Sage session where r was initially the R interpreter, but was then redefined to be something else, this can also be used, instead of reset('r'), to restore r as the R interpreter.

edit flag offensive delete link more


Thank you for your answer. I am using Sage via CoCalc. When I open a new Sage worksheet and type:


x = r([10.4,5.6,3.1,6.4,21.7]); x

I get:

<function r="" at="" 0x7fd8f6b5f5f0="">

Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/", line 1013, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/cocalc/lib/python2.7/site-packages/smc_sagews/", line 2168, in r return r.jupyter_kernel(code,**kwargs) File "/cocalc/lib/python2.7/site-packages/smc_sagews/", line 179, in run_code msg_id = kc.execute(code) File "/ext/sage/sage-8.0/local/lib/python2.7/site-packages/jupyter_client/", line 244, in execute raise ...

Kristen Barth gravatar imageKristen Barth ( 2017-12-09 15:28:10 +0200 )edit

... ValueError('code %r must be a string' % code) ValueError: code [10.4000000000000, 5.60000000000000, 3.10000000000000, 6.40000000000000, 21.7000000000000] must be a string

Kristen Barth gravatar imageKristen Barth ( 2017-12-09 15:30:10 +0200 )edit

Ok, when I use it works perfectly fine.

Kristen Barth gravatar imageKristen Barth ( 2017-12-09 16:25:58 +0200 )edit

Please click "help" at the very top of your cocalc worksheet and explain your problem there. The CoCalc team will be able to see the worksheet where you are having problem and to help you out.

slelievre gravatar imageslelievre ( 2017-12-09 18:53:58 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2017-12-08 18:07:11 +0200

Seen: 502 times

Last updated: Jul 07