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.
> --
> 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
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
>
--
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.
> 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.