Some R integration samples - is there better way to do it?

16 views
Skip to first unread message

Andrzej Giniewicz

unread,
Jul 4, 2010, 11:54:07 AM7/4/10
to sage-s...@googlegroups.com
Hi,

recently I have given short introductory talk about Sage and questions
about integration with R arisen. Results of quickly put together code
is now at http://sagenb.org/home/pub/2232/ - especially, the question
was: "I have variable named x in R environment and want to get its
value, or variable named x inside Sage environment and push its value
into R, but keep the name." - what I was able to think about is quick
hack with .GlobalEnv of R, but is there better way to do what is
presented in the notebook linked above? Is there some syntax like
r["x"]=[1,2,3] or sth? If there is, I haven't noticed it yet - would
be thankful for hints.

Cheers,
Andrzej.

Tim Joseph Dumol

unread,
Jul 4, 2010, 12:13:07 PM7/4/10
to sage-s...@googlegroups.com
Hi Andrezj,

> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>

This should do what you wanted:

sage: r('x <- c(1,2,3)')
[1] 1 2 3
sage: r('x')
[1] 1 2 3
sage: sageobj(r('x')) # r('x').sage() works too
[1, 2, 3]
sage: r([1, 2, 3])
[1] 1 2 3
---
Tim Joseph Dumol <tim (at) timdumol (dot) com>
http://timdumol.com

Tim Joseph Dumol

unread,
Jul 4, 2010, 12:15:53 PM7/4/10
to sage-s...@googlegroups.com

Oh, and:

sage: r.set('y', r([1,2,3]))
sage: r('y')
[1] 1 2 3

Try:

sage: r.<TAB>

for more commands.


> ---
> Tim Joseph Dumol <tim (at) timdumol (dot) com>
> http://timdumol.com
>

--

Andrzej Giniewicz

unread,
Jul 7, 2010, 5:02:18 AM7/7/10
to sage-s...@googlegroups.com
ah, thanks - I know about r.tab, well - most of it are R commands, the
issue is that I didn't found those sage specific ones - it would help
if at least list of modified commands would appear in "r?" docstrings.
Anyway, is r.set working? I used:

sage: r.ls()
character(0)


sage: r.set('y', r([1,2,3]))

sage: r.ls()
[1] "sage0" "sage1" "sage2" "sage3" "sage4"

and there is no variable 'y' in R env. (using sage 4.4.2 here, was it
fixed/added/changed later on?) - anyway, variable 'y' is inserted when
I used:

%r
letSage <- function(variable,value) { .GlobalEnv[[variable]]<-value }

def setR(var, val):
r.letSage('"%s"'%var, r(val))

and called it with:

setR('y', [1,2,3])

so it still isn't what I look for, thout the "r(...)" for "val" helped
to simplify it a lot. The r('var <- val') works, but requires to
specify val as R code, so it's not best way I think. I'm looking for
something you can pass list as argument, and preferably without r code
and function above. The r.set if it worked would be nice I think :)

cheers,
Andrzej.

kcrisman

unread,
Jul 7, 2010, 10:07:29 AM7/7/10
to sage-support


On Jul 7, 5:02 am, Andrzej Giniewicz <ggi...@gmail.com> wrote:
> ah, thanks - I know about r.tab, well - most of it are R commands, the
> issue is that I didn't found those sage specific ones - it would help
> if at least list of modified commands would appear in "r?" docstrings.
> Anyway, is r.set working? I used:
>
> sage: r.ls()
> character(0)
> sage: r.set('y', r([1,2,3]))
> sage: r.ls()
> [1] "sage0" "sage1" "sage2" "sage3" "sage4"
>
Dear Andrzej,

Thanks for your interest in our R interface. We really appreciate
actual practical experience with it.

Note the following in

sage: r.set?

Definition: r.set(self, var, value)
Docstring:
Set the variable var in R to what the string value evaluates to
in
R.

INPUT:
var -- a string value -- a string

EXAMPLES:
sage: r.set('a', '2 + 3') sage: r.get('a') '[1] 5'


There should be interactive help (using ?) for most methods. Anyway,
r([1,2,3]) probably doesn't evaluate to anything in R.

sage: r.set('y','c(1,2,3)')
sage: r.ls()
[1] "y"
sage: r.get('y')
'[1] 1 2 3'


Is this what you are thinking of? Basically, one can either use
things like r.eval and r.set to do things in R, like so

sage:
r.eval('kruskal.test(list(c(18,16,14,9,7,2),c(17,15,12,11,10,1),c(13,8,6,5,4,3)))')
'\n\tKruskal-Wallis rank sum test\n\ndata: list(c(18, 16, 14, 9, 7,
2), c(17, 15, 12, 11, 10, 1), c(13, 8, 6, 5, 4, 3)) \nKruskal-
Wallis chi-squared = 2.8421, df = 2, p-value = 0.2415\n'

but unfortunately not

sage: r.kruskal_test('[[18,11,7,6,5,1],[17,16,15,10,9,8],
[14,13,12,4,3,2]]')Error: object 'sage4' not found

even though many other r.method() things do work very nicely. The
problem is that so much R stuff wants a 'c()'-style vector/list but
it's not always easy to figure out how to get the interface to accept
your input. Keywords actually often work.

So I think that no one would disagree that the R integration is not
perfect yet. You can use rpy, perhaps:

sage: from rpy2 import [tab]
rinterface robjects tests
rlike rpy_classic tests_rpy_classic

but this may not be what you are looking for either.

However, lots of stuff should work fairly well this way. We would
welcome good examples of how to use the R interface more efficiently,
especially with other subsystems of Sage. I have used R reasonably
well with the combinatorics facilities in Sage, for instance.

Thanks!

- kcrisman
> and there is no variable 'y' in R env. (using sage 4.4.2 here, was it
> fixed/added/changed later on?) - anyway, variable 'y' is inserted when
> I used:
>
> %r
> letSage <- function(variable,value) { .GlobalEnv[[variable]]<-value }
>
> def setR(var, val):
>     r.letSage('"%s"'%var, r(val))
>
> and called it with:
>
> setR('y', [1,2,3])
>
> so it still isn't what I look for, thout the "r(...)" for "val" helped
> to simplify it a lot. The r('var <- val') works, but requires to
> specify val as R code, so it's not best way I think. I'm looking for
> something you can pass list as argument, and preferably without r code
> and function above. The r.set if it worked would be nice I think :)
>
> cheers,
> Andrzej.
>
> On Sun, Jul 4, 2010 at 6:15 PM, Tim Joseph Dumol <t...@timdumol.com> wrote:
>
>
>
> > On Mon, Jul 5, 2010 at 12:13 AM, Tim Joseph Dumol <t...@timdumol.com> wrote:
> >> Hi Andrezj,
>
> >> On Sun, Jul 4, 2010 at 11:54 PM, Andrzej Giniewicz <ggi...@gmail.com> wrote:
> >>> Hi,
>
> >>> recently I have given short introductory talk about Sage and questions
> >>> about integration with R arisen. Results of quickly put together code
> >>> is now athttp://sagenb.org/home/pub/2232/- especially, the question
> >>> was: "I have variable named x in R environment and want to get its
> >>> value, or variable named x inside Sage environment and push its value
> >>> into R, but keep the name." - what I was able to think about is quick
> >>> hack with .GlobalEnv of R, but is there better way to do what is
> >>> presented in the notebook linked above? Is there some syntax like
> >>> r["x"]=[1,2,3] or sth? If there is, I haven't noticed it yet - would
> >>> be thankful for hints.
>
> >>> Cheers,
> >>> Andrzej.
>
> >>> --
> >>> To post to this group, send email to sage-s...@googlegroups.com
> >>> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> >>> For more options, visit this group athttp://groups.google.com/group/sage-support

Andrzej Giniewicz

unread,
Jul 7, 2010, 10:38:11 AM7/7/10
to sage-s...@googlegroups.com
Hi,

> Note the following in
>
> sage: r.set?
>
> Definition:     r.set(self, var, value)
> Docstring:
>       Set the variable var in R to what the string value evaluates to
> in
>       R.
>
>       INPUT:
>          var -- a string value -- a string
>
>       EXAMPLES:
>          sage: r.set('a', '2 + 3') sage: r.get('a') '[1] 5'
>
>
> There should be interactive help (using ?) for most methods.  Anyway,
> r([1,2,3]) probably doesn't evaluate to anything in R.

I know - read docstring for r.set, but I tried to use it to follow
Tims example anyway:

sage: r.set('y', r([1,2,3]))
sage: r('y')
[1] 1 2 3

I understood from Tims sample that r.set("variable", r(value)) would
set it. Anyway, the thing I want to do is function taking variable
name and pythonic list or value and using it in R to set variable
value. Exactly stuff that is done by below code.

in R:


letSage <- function(variable,value) { .GlobalEnv[[variable]]<-value }

in Sage:


def setR(var, val):
r.letSage('"%s"'%var, r(val))

setR('y', [1,2,3])
setR('z', [1,'"ok"',3])
instead of mostly equivalent but with R syntax of vectors


r.set('y', 'c(1,2,3)')

r.set('z', 'c(1,"ok",3)')

So the syntax r.set('var', r(val)) is meant to work? I think that way
to set variable in R environment to normal list returned by other
function would improve integration a lot, at least remove some
intermediate steps.

If we are at your sample I'm tottaly aware that '[1,2,3]' would not
work, or r('[1,2,3]') - but it is r([1,2,3]) in question that
evaluates to RElement "[1] 1 2 3" and is stored in one of temporary
sage* variables, and as sample with setR shows, can be used to pass
around python list, strings and stuff.

cheers,
Andrzej.

Reply all
Reply to author
Forward
0 new messages