Ask Your Question
6

How to use R with Sage?

asked 2011-01-16 18:50:16 +0100

dartdog gravatar image

updated 2011-04-28 17:02:37 +0100

Kelvin Li gravatar image

Just loaded the Sage system, went through tutorial (very quickly) searched around, I see it says it has R but I don't see any reference to it in any docs?? I was hoping to use Sage as A notebook type interface to R? mainly for doing exploratory data analysis or something like that...

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
6

answered 2011-01-16 19:49:50 +0100

Mike Hansen gravatar image

If you start the Sage notebook, you can set the "system" of the notebook R by selecting "r" on the system dropdown. (The system dropdown is located toward the top of the screen in the notebook just to the right of the dropdown labeled "Data...". Initially, it will be set to "sage".)

Additionally, you can set the system of an individual cell to R by putting "%r" at the top of the cell.

When in R-mode, all of the commands that you enter into the notebook will be sent directly to a copy of R. For example, you can evaluate the following in the notebook:

a <- c(1,2,3);
a*2

to get the output

[1] 2 4 6

If you want to use R to do plotting in the notebook, then you need to use the PNG device. Here is some code that produces a basic plot:

png(file="myplot.png", bg="transparent")
plot(1:10)
rect(1, 5, 3, 7, col="white")
dev.off()

When R writes the image to a file, the notebook detects that a file has been created and displays it in the notebook.

edit flag offensive delete link more

Comments

So today I learned that R, which I use occasionally, is included in Sage and how to use it in the notebook. Great. Much appreciated.

mouse gravatar imagemouse ( 2011-01-17 00:29:42 +0100 )edit

Didn't work for me on Sage 6.1.1. But I found to be due to a bug in the `png` function on R 3.0.2

fccoelho gravatar imagefccoelho ( 2014-08-06 16:25:53 +0100 )edit

Thank you for that plotting command. I couldn't get plot(x,y) to work but saving to png on Ubuntu works. Cheers

NahsiN gravatar imageNahsiN ( 2015-06-27 23:35:14 +0100 )edit
1

answered 2011-01-17 12:37:21 +0100

kcrisman gravatar image

Mike's answer is great. It's also worth pointing out a couple other things.

  1. In Mac, you may need to use the quartz device instead, because it is very difficult to configure Macs properly to use the png device, so we don't do it.
  2. You can also do quite a bit of R from Sage using the r.[tab] family of commands. This tries to streamline the interface a little. For some reason this is not yet in the standard reference, but

    sage: sage.interfaces.r??

will give you a fair amount of information. You may also want to see my talk from useR! 2010 to see a few other tricks. It's definitely possible to pass R and Sage stuff back and forth, especially if you don't need blazing speed.

Unfortunately, there is still a lot of work to be done to get R types and Sage syntax to be really easy to mix. Maybe you can help?

edit flag offensive delete link more

Comments

Thank you both I'll give it a go shortly

dartdog gravatar imagedartdog ( 2011-02-03 11:50:34 +0100 )edit

Hi sorry but I did not find the way to enable R, is this also available in Sage Math for windows ?

The system dropdown is located toward the top of the screen in the notebook just to the right of the dropdown labeled "Data...". Initially, it will be set to "sage".)

what is and where is this "system drop down" ?, once you have a notebook cell displayed ?

when I enter %r I got

 File "<string>", line unknown
SyntaxError: r command required
ortollj gravatar imageortollj ( 2017-12-31 19:37:19 +0100 )edit

If you are using the Jupyter notebook (the default on Windows now, I think) then there is a place for "kernel", which could be R. These instructions are for the Sage notebook sagenb.

kcrisman gravatar imagekcrisman ( 2018-01-04 22:42:53 +0100 )edit
1

Note: the worksheet from @kcrisman's 2010 talk has been converted into of a .sagews document available on CoCalc at

The sws -> sagews conversion might not be perfect but at least allows to recover the worksheet in some form.

slelievre gravatar imageslelievre ( 2018-11-16 20:09:52 +0100 )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

1 follower

Stats

Asked: 2011-01-16 18:50:16 +0100

Seen: 6,924 times

Last updated: Jan 17 '11