Ask Your Question
5

How to use R with Sage?

asked 2011-01-16 11:50:16 -0600

dartdog gravatar image

updated 2011-04-28 10:02:37 -0600

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 12:49:50 -0600

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-16 17:29:42 -0600 )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 09:25:53 -0600 )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 16:35:14 -0600 )edit
1

answered 2011-01-17 05:37:21 -0600

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 04:50:34 -0600 )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 11:50:16 -0600

Seen: 4,072 times

Last updated: Jan 17 '11