ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 16 Nov 2018 20:09:52 +0100How to use R with Sage?https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/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...Sun, 16 Jan 2011 18:50:16 +0100https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/Answer by Mike Hansen for <p>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...</p>
https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?answer=11980#post-id-11980If 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.Sun, 16 Jan 2011 19:49:50 +0100https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?answer=11980#post-id-11980Comment by NahsiN for <p>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".)</p>
<p>Additionally, you can set the system of an individual cell to R by putting "%r" at the top of the cell.</p>
<p>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:</p>
<pre><code>a <- c(1,2,3);
a*2
</code></pre>
<p>to get the output</p>
<pre><code>[1] 2 4 6
</code></pre>
<p>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:</p>
<pre><code>png(file="myplot.png", bg="transparent")
plot(1:10)
rect(1, 5, 3, 7, col="white")
dev.off()
</code></pre>
<p>When R writes the image to a file, the notebook detects that a file has been created and displays it in the notebook.</p>
https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=27214#post-id-27214Thank you for that plotting command. I couldn't get plot(x,y) to work but saving to png on Ubuntu works. CheersSat, 27 Jun 2015 23:35:14 +0200https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=27214#post-id-27214Comment by fccoelho for <p>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".)</p>
<p>Additionally, you can set the system of an individual cell to R by putting "%r" at the top of the cell.</p>
<p>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:</p>
<pre><code>a <- c(1,2,3);
a*2
</code></pre>
<p>to get the output</p>
<pre><code>[1] 2 4 6
</code></pre>
<p>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:</p>
<pre><code>png(file="myplot.png", bg="transparent")
plot(1:10)
rect(1, 5, 3, 7, col="white")
dev.off()
</code></pre>
<p>When R writes the image to a file, the notebook detects that a file has been created and displays it in the notebook.</p>
https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=23702#post-id-23702Didn'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.2Wed, 06 Aug 2014 16:25:53 +0200https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=23702#post-id-23702Comment by mouse for <p>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".)</p>
<p>Additionally, you can set the system of an individual cell to R by putting "%r" at the top of the cell.</p>
<p>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:</p>
<pre><code>a <- c(1,2,3);
a*2
</code></pre>
<p>to get the output</p>
<pre><code>[1] 2 4 6
</code></pre>
<p>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:</p>
<pre><code>png(file="myplot.png", bg="transparent")
plot(1:10)
rect(1, 5, 3, 7, col="white")
dev.off()
</code></pre>
<p>When R writes the image to a file, the notebook detects that a file has been created and displays it in the notebook.</p>
https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=22271#post-id-22271So today I learned that R, which I use occasionally, is included in Sage and how to use it in the notebook. Great. Much appreciated.Mon, 17 Jan 2011 00:29:42 +0100https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=22271#post-id-22271Answer by kcrisman for <p>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...</p>
https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?answer=11983#post-id-11983Mike'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](http://www.sagenb.org/home/pub/2270) 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?
Mon, 17 Jan 2011 12:37:21 +0100https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?answer=11983#post-id-11983Comment by ortollj for <p>Mike's answer is great. It's also worth pointing out a couple other things.</p>
<ol>
<li>In Mac, you may need to use the <code>quartz</code> device instead, because it is very difficult to configure Macs properly to use the <code>png</code> device, so we don't do it.</li>
<li><p>You can also do quite a bit of R from Sage using the <code>r.[tab]</code> family of commands. This tries to streamline the interface a little. For some reason this is not yet in the standard reference, but </p>
<p>sage: sage.interfaces.r??</p></li>
</ol>
<p>will give you a fair amount of information. You may also want to see <a href="http://www.sagenb.org/home/pub/2270">my talk from useR! 2010</a> 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.</p>
<p>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?</p>
https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=40402#post-id-40402Hi
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 requiredSun, 31 Dec 2017 19:37:19 +0100https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=40402#post-id-40402Comment by kcrisman for <p>Mike's answer is great. It's also worth pointing out a couple other things.</p>
<ol>
<li>In Mac, you may need to use the <code>quartz</code> device instead, because it is very difficult to configure Macs properly to use the <code>png</code> device, so we don't do it.</li>
<li><p>You can also do quite a bit of R from Sage using the <code>r.[tab]</code> family of commands. This tries to streamline the interface a little. For some reason this is not yet in the standard reference, but </p>
<p>sage: sage.interfaces.r??</p></li>
</ol>
<p>will give you a fair amount of information. You may also want to see <a href="http://www.sagenb.org/home/pub/2270">my talk from useR! 2010</a> 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.</p>
<p>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?</p>
https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=40463#post-id-40463If 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.Thu, 04 Jan 2018 22:42:53 +0100https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=40463#post-id-40463Comment by slelievre for <p>Mike's answer is great. It's also worth pointing out a couple other things.</p>
<ol>
<li>In Mac, you may need to use the <code>quartz</code> device instead, because it is very difficult to configure Macs properly to use the <code>png</code> device, so we don't do it.</li>
<li><p>You can also do quite a bit of R from Sage using the <code>r.[tab]</code> family of commands. This tries to streamline the interface a little. For some reason this is not yet in the standard reference, but </p>
<p>sage: sage.interfaces.r??</p></li>
</ol>
<p>will give you a fair amount of information. You may also want to see <a href="http://www.sagenb.org/home/pub/2270">my talk from useR! 2010</a> 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.</p>
<p>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?</p>
https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=44312#post-id-44312Note: the worksheet from @kcrisman's 2010 talk has been converted into
of a .sagews document available on CoCalc at
- [https://share.cocalc.com/share/19575ea0-317e-402b-be57-368d04c113db/pub/2201-2301/2270.sagews?viewer=share](https://share.cocalc.com/share/19575ea0-317e-402b-be57-368d04c113db/pub/2201-2301/2270.sagews?viewer=share)
The sws -> sagews conversion might not be perfect but at least
allows to recover the worksheet in some form.Fri, 16 Nov 2018 20:09:52 +0100https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=44312#post-id-44312Comment by dartdog for <p>Mike's answer is great. It's also worth pointing out a couple other things.</p>
<ol>
<li>In Mac, you may need to use the <code>quartz</code> device instead, because it is very difficult to configure Macs properly to use the <code>png</code> device, so we don't do it.</li>
<li><p>You can also do quite a bit of R from Sage using the <code>r.[tab]</code> family of commands. This tries to streamline the interface a little. For some reason this is not yet in the standard reference, but </p>
<p>sage: sage.interfaces.r??</p></li>
</ol>
<p>will give you a fair amount of information. You may also want to see <a href="http://www.sagenb.org/home/pub/2270">my talk from useR! 2010</a> 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.</p>
<p>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?</p>
https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=22199#post-id-22199Thank you both I'll give it a go shortly Thu, 03 Feb 2011 11:50:34 +0100https://ask.sagemath.org/question/7805/how-to-use-r-with-sage/?comment=22199#post-id-22199