It's definitely possible.  It's also somewhat annoying.  If you
actually have made an R module, you could do

r.install_packages(stuff with the repo mentioned)
r.library('my_module')

and that might be the easiest way to gain access.  Unfortunately, it's
not very easy to use user-defined functions (or R library ones) with
optional arguments using the r.foo() syntax.

Here is an example of something I've (successfully) used.  I've
removed stuff that isn't about getting stuff out of R to make it more
readable.  I've already installed and loaded the orloca and depth
libraries of R here, so you are seeing the sort of syntax one has to
use, particularly because R doesn't always return things in Python-
friendly packages when one uses r.eval()

Good luck!

def MCBC(prof,algorithm='w'): # the function that gets the
mediancentres from a profile
    if algorithm in ['w','g']: # Weiszfeldt or gradient methods
        for k in range(6):
            exes += [cos(k*npi/3)]
            whys += [sin(k*npi/3)]
        Z =
r.eval('zsummin(loca.p(c(%s),c(%s),c(%s)),max.iter=400,algorithm="\
%s")'%(str(exes)[1:-1],str(whys)[1:-1],str(prof)[1:-1],algorithm))
        Z = Z.split()
        return (RR(Z[1]),RR(Z[2]))
    if algorithm in ['depth','depth2']:
        for k in range(6):
            exes += prof[k]*[cos(k*npi/3)]
            whys += prof[k]*[sin(k*npi/3)]
        if algorithm=='depth':
            Z =
r.eval('med(matrix(c(%s),ncol=2),maxit=400,method="Spatial")'%str(exes
+whys)[1:-1]) # Note optional arguments maxit=200 and eps=1e-8 can be
changed
        if algorithm=='depth2':
            r.eval('spamed=function(x){list(median
= .Fortran("medctr78", as.numeric(x), med = numeric(length(x[1, ])),
as.integer(length(x[, 1])), as.integer(length(x[1, ])), integer(1),
err = integer(1), PACKAGE = "depth")$med)}')
            Z = r.eval('spamed(matrix(c(%s),ncol=2))'%str(exes+whys)
[1:-1])
        Z = Z.splitlines()[1].split() # this returns only the [1] 1 0
part of the string as a list of strings
        return (RR(Z[1]),RR(Z[2])) # do something like this to account
for floating point error, though certainly this is one of the most
naive things one could do




On Mar 21, 2:08 pm, Kirill Vankov <kirill.van...@gmail.com> wrote:
> Suppose I have some R code with some functions defined.  I would like
> to be able to call these functions within SAGE.   I did not find any
> examples of someone doing so and I was not able to do so myself...
>
> I understand how to initiate R session within SAGE and within notebook
> cell, however, it is not what I want.  I want to create variables in
> SAGE, call my own R function passing SAGE variables and use the
> outputs again in SAGE.  There are quite many functions in my R
> library, therefore I want to avoid rewriting all the code in SAGE.
>
> Any hints how to do that?

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

Reply via email to