ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 11 Nov 2011 15:35:44 -0600Converting R variables to sagehttp://ask.sagemath.org/question/8375/converting-r-variables-to-sage/If I have a bunch of notebook cells with R code using the %r method, is there an easy way to grab the data as a varible in Python/Sage without converting the whole thing to use r.command syntax?
Thu, 13 Oct 2011 09:25:06 -0500http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/Answer by kcrisman for <p>If I have a bunch of notebook cells with R code using the %r method, is there an easy way to grab the data as a varible in Python/Sage without converting the whole thing to use r.command syntax?</p>
http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?answer=12761#post-id-12761You at least have access to them, though only in printed format. Here's a typical example from the worksheet (go to "Edit" to see this, but it looks fine in the notebook).
{{{id=51|
%r
a <- c(1,2,3)
///
}}}
{{{id=52|
%r
a
///
[1] 1 2 3
}}}
{{{id=58|
r.get('a')
///
'[1] 1 2 3'
}}}
I have a feeling there is another way to do this, though. The key is using _getitem_ but for RElements instead. I haven't quite nailed it down, and I have to go now, but this may be possible. Fri, 14 Oct 2011 11:15:10 -0500http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?answer=12761#post-id-12761Answer by kcrisman for <p>If I have a bunch of notebook cells with R code using the %r method, is there an easy way to grab the data as a varible in Python/Sage without converting the whole thing to use r.command syntax?</p>
http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?answer=12762#post-id-12762Victory! I knew this should be possible.
In the notebook:
%r
a <- c(1,2,3)
a
[1] 1 2 3
from sage.interfaces.r import RElement
b = RElement(r,'a')
c = b._sage_()
c
[1, 2, 3]
and then you can do stuff to `c` because it's not an RElement.
c[1] = 3
c
[1, 3, 3]
I don't know whether this is worth wrapping slightly better, or if that is even possible. But hopefully this works for you!Fri, 14 Oct 2011 16:33:33 -0500http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?answer=12762#post-id-12762Comment by kcrisman for <p>Victory! I knew this should be possible.</p>
<p>In the notebook:</p>
<pre><code>%r
a <- c(1,2,3)
a
[1] 1 2 3
from sage.interfaces.r import RElement
b = RElement(r,'a')
c = b._sage_()
c
[1, 2, 3]
</code></pre>
<p>and then you can do stuff to <code>c</code> because it's not an RElement.</p>
<pre><code>c[1] = 3
c
[1, 3, 3]
</code></pre>
<p>I don't know whether this is worth wrapping slightly better, or if that is even possible. But hopefully this works for you!</p>
http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?comment=21121#post-id-21121Note this will only work with data that actually converts nicely into Sage/Python structures. For instance, I don't know whether data frames convert nicely into arrays or something.Fri, 14 Oct 2011 16:34:33 -0500http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?comment=21121#post-id-21121Answer by Jason Grout for <p>If I have a bunch of notebook cells with R code using the %r method, is there an easy way to grab the data as a varible in Python/Sage without converting the whole thing to use r.command syntax?</p>
http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?answer=12753#post-id-12753Probably the easiest way is to write the data out to a file in the R cell and read it in using python in a Sage cell. Make sure to write it out to the DATA directory, which you can find by doing "print DATA" in a Sage cell.Thu, 13 Oct 2011 11:18:37 -0500http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?answer=12753#post-id-12753Comment by mlanting for <p>Probably the easiest way is to write the data out to a file in the R cell and read it in using python in a Sage cell. Make sure to write it out to the DATA directory, which you can find by doing "print DATA" in a Sage cell.</p>
http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?comment=21124#post-id-21124hmm.. I was hoping for a more direct variable conversion, but that will work. Thank you!Fri, 14 Oct 2011 05:20:25 -0500http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?comment=21124#post-id-21124Answer by Mike Hansen for <p>If I have a bunch of notebook cells with R code using the %r method, is there an easy way to grab the data as a varible in Python/Sage without converting the whole thing to use r.command syntax?</p>
http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?answer=12882#post-id-12882If you've done
%r
a <- c(1,2,3)
in one notebook cell. You can just do
r('a')._sage_()
to get back the list `[1, 2, 3]`. This is a bit easier than using RElement.
Fri, 11 Nov 2011 10:23:04 -0600http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?answer=12882#post-id-12882Comment by kcrisman for <p>If you've done</p>
<pre><code>%r
a <- c(1,2,3)
</code></pre>
<p>in one notebook cell. You can just do</p>
<pre><code>r('a')._sage_()
</code></pre>
<p>to get back the list <code>[1, 2, 3]</code>. This is a bit easier than using RElement.</p>
http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?comment=20920#post-id-20920Hah, do I look silly now! Can you think of any even simpler syntax that would be consistent with the rest of Sage? At the very least we should document this notebook behavior clearly in `r?`.Fri, 11 Nov 2011 15:35:44 -0600http://ask.sagemath.org/question/8375/converting-r-variables-to-sage/?comment=20920#post-id-20920