Re: [R] rpy2 and user defined functions from R

2013-10-30 Thread Wensui Liu
if you don't need to exchange big data between r and python, pyper might be
better than rpy2.
On Oct 30, 2013 12:08 AM, "Erin Hodgess"  wrote:

> Hello again!
>
> I'm using python with a module rpy2 to call functions from R.
>
> It works fine on built in R functions like rnorm.
>
> However, I would like to access user-defined functions as well.  For those
> of you who use this, I have:
>
> import rpy2.robjects as R
> x = R.r.buzz(3)
> R object as no attribute buzz
>
> (user defined function of buzz)
>
> This is on a Centos 5 machine with R-3.0.2 and python of 2.7.5.
>
> Thanks for any help.
> Sincerely,
> Erin
>
>
>
> --
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodg...@gmail.com
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] rpy2 and user defined functions from R

2013-10-30 Thread Collin Lynch
I don't believe that rpy2 will load a saved workspace.  When I have worked
with this I always load my functions by sourcing an r file separately:

R.r['source'](MyFuncs.r)


Best,
Collin.

On Wed, 30 Oct 2013, Erin Hodgess wrote:

> Here we go:
>
> > buzz
> function(x) {
> y <- x + pi
> return(y)
> }
> > q()
> Save workspace image? [y/n/c]: python
> Save workspace image? [y/n/c]: y
> root@erinminfo [/home/erinminf/public_html]# python
> Python 2.7.5 (default, Sep 11 2013, 02:14:06)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import rpy2.robjects as R
> >>> R.r.buzz(3)
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/local/lib/python2.7/site-packages/rpy2/robjects/__init__.py",
> line 213, in __getattribute__
> raise orig_ae
> AttributeError: 'R' object has no attribute 'buzz'
> >>> R.r['buzz'](3)
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/local/lib/python2.7/site-packages/rpy2/robjects/__init__.py",
> line 216, in __getitem__
> res = _globalenv.get(item)
> LookupError: 'buzz' not found
> >>>
> root@erinminfo [/home/erinminf/public_html]#
>
>
> On Wed, Oct 30, 2013 at 10:16 AM, Collin Lynch  wrote:
>
> > Erin, one question, can you access the defined functions by key?
> >
> > In lieu of:
> > > x = R.r.buzz(3)
> >
> > Can you do:
> >   x = R.r['buzz'](3)
> >
> >
> > Alternatively if you need only one or two custom functions have you
> > considered just defining them via python as in:
> >
> > PStr = """
> > function(LM) {
> >   S <- summary(LM);
> >   print(S$fstatistic);
> >   F <- S$fstatistic;
> >   P <- pf(F[1], F[2], F[3], lower=FALSE);
> >   return(P);
> > }
> > """
> > r_LMPValFunc = robjects.r(PStr)
> >
> > Best,
> > Collin.
> >
> >
> > On Tue, 29 Oct 2013, Erin Hodgess wrote:
> >
> > > Hello again!
> > >
> > > I'm using python with a module rpy2 to call functions from R.
> > >
> > > It works fine on built in R functions like rnorm.
> > >
> > > However, I would like to access user-defined functions as well.  For
> > those
> > > of you who use this, I have:
> > >
> > > import rpy2.robjects as R
> > > R object as no attribute buzz
> > >
> > > (user defined function of buzz)
> > >
> > > This is on a Centos 5 machine with R-3.0.2 and python of 2.7.5.
> > >
> > > Thanks for any help.
> > > Sincerely,
> > > Erin
> > >
> > >
> > >
> > > --
> > > Erin Hodgess
> > > Associate Professor
> > > Department of Computer and Mathematical Sciences
> > > University of Houston - Downtown
> > > mailto: erinm.hodg...@gmail.com
> > >
> > >   [[alternative HTML version deleted]]
> > >
> > > __
> > > R-help@r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> >
>
>
> --
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodg...@gmail.com
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] rpy2 and user defined functions from R

2013-10-30 Thread Erin Hodgess
Solve:

I wrote the buzz function to buzz.R

And now I have:

from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage as
STAP
with open("buzz.R","r") as f:
   string = '''.join(f.readlines())
buzz = STAP(string,"buzz")
buzz.buzz(3)

And all is well!

Thanks,
Erin





On Wed, Oct 30, 2013 at 11:05 AM, Erin Hodgess wrote:

> Here we go:
>
> > buzz
> function(x) {
> y <- x + pi
> return(y)
> }
> > q()
> Save workspace image? [y/n/c]: python
> Save workspace image? [y/n/c]: y
> root@erinminfo [/home/erinminf/public_html]# python
> Python 2.7.5 (default, Sep 11 2013, 02:14:06)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import rpy2.robjects as R
> >>> R.r.buzz(3)
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/local/lib/python2.7/site-packages/rpy2/robjects/__init__.py",
> line 213, in __getattribute__
> raise orig_ae
> AttributeError: 'R' object has no attribute 'buzz'
> >>> R.r['buzz'](3)
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/local/lib/python2.7/site-packages/rpy2/robjects/__init__.py",
> line 216, in __getitem__
> res = _globalenv.get(item)
> LookupError: 'buzz' not found
> >>>
> root@erinminfo [/home/erinminf/public_html]#
>
>
> On Wed, Oct 30, 2013 at 10:16 AM, Collin Lynch wrote:
>
>> Erin, one question, can you access the defined functions by key?
>>
>> In lieu of:
>> > x = R.r.buzz(3)
>>
>> Can you do:
>>   x = R.r['buzz'](3)
>>
>>
>> Alternatively if you need only one or two custom functions have you
>> considered just defining them via python as in:
>>
>> PStr = """
>> function(LM) {
>>   S <- summary(LM);
>>   print(S$fstatistic);
>>   F <- S$fstatistic;
>>   P <- pf(F[1], F[2], F[3], lower=FALSE);
>>   return(P);
>> }
>> """
>> r_LMPValFunc = robjects.r(PStr)
>>
>> Best,
>> Collin.
>>
>>
>> On Tue, 29 Oct 2013, Erin Hodgess wrote:
>>
>> > Hello again!
>> >
>> > I'm using python with a module rpy2 to call functions from R.
>> >
>> > It works fine on built in R functions like rnorm.
>> >
>> > However, I would like to access user-defined functions as well.  For
>> those
>> > of you who use this, I have:
>> >
>> > import rpy2.robjects as R
>> > R object as no attribute buzz
>> >
>> > (user defined function of buzz)
>> >
>> > This is on a Centos 5 machine with R-3.0.2 and python of 2.7.5.
>> >
>> > Thanks for any help.
>> > Sincerely,
>> > Erin
>> >
>> >
>> >
>> > --
>> > Erin Hodgess
>> > Associate Professor
>> > Department of Computer and Mathematical Sciences
>> > University of Houston - Downtown
>> > mailto: erinm.hodg...@gmail.com
>> >
>> >   [[alternative HTML version deleted]]
>> >
>> > __
>> > R-help@r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>>
>
>
> --
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodg...@gmail.com
>



-- 
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: erinm.hodg...@gmail.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] rpy2 and user defined functions from R

2013-10-30 Thread Erin Hodgess
Here we go:

> buzz
function(x) {
y <- x + pi
return(y)
}
> q()
Save workspace image? [y/n/c]: python
Save workspace image? [y/n/c]: y
root@erinminfo [/home/erinminf/public_html]# python
Python 2.7.5 (default, Sep 11 2013, 02:14:06)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpy2.robjects as R
>>> R.r.buzz(3)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.7/site-packages/rpy2/robjects/__init__.py",
line 213, in __getattribute__
raise orig_ae
AttributeError: 'R' object has no attribute 'buzz'
>>> R.r['buzz'](3)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.7/site-packages/rpy2/robjects/__init__.py",
line 216, in __getitem__
res = _globalenv.get(item)
LookupError: 'buzz' not found
>>>
root@erinminfo [/home/erinminf/public_html]#


On Wed, Oct 30, 2013 at 10:16 AM, Collin Lynch  wrote:

> Erin, one question, can you access the defined functions by key?
>
> In lieu of:
> > x = R.r.buzz(3)
>
> Can you do:
>   x = R.r['buzz'](3)
>
>
> Alternatively if you need only one or two custom functions have you
> considered just defining them via python as in:
>
> PStr = """
> function(LM) {
>   S <- summary(LM);
>   print(S$fstatistic);
>   F <- S$fstatistic;
>   P <- pf(F[1], F[2], F[3], lower=FALSE);
>   return(P);
> }
> """
> r_LMPValFunc = robjects.r(PStr)
>
> Best,
> Collin.
>
>
> On Tue, 29 Oct 2013, Erin Hodgess wrote:
>
> > Hello again!
> >
> > I'm using python with a module rpy2 to call functions from R.
> >
> > It works fine on built in R functions like rnorm.
> >
> > However, I would like to access user-defined functions as well.  For
> those
> > of you who use this, I have:
> >
> > import rpy2.robjects as R
> > R object as no attribute buzz
> >
> > (user defined function of buzz)
> >
> > This is on a Centos 5 machine with R-3.0.2 and python of 2.7.5.
> >
> > Thanks for any help.
> > Sincerely,
> > Erin
> >
> >
> >
> > --
> > Erin Hodgess
> > Associate Professor
> > Department of Computer and Mathematical Sciences
> > University of Houston - Downtown
> > mailto: erinm.hodg...@gmail.com
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>


-- 
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: erinm.hodg...@gmail.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] rpy2 and user defined functions from R

2013-10-30 Thread Collin Lynch
Erin, one question, can you access the defined functions by key?

In lieu of:
> x = R.r.buzz(3)

Can you do:
  x = R.r['buzz'](3)


Alternatively if you need only one or two custom functions have you
considered just defining them via python as in:

PStr = """
function(LM) {
  S <- summary(LM);
  print(S$fstatistic);
  F <- S$fstatistic;
  P <- pf(F[1], F[2], F[3], lower=FALSE);
  return(P);
}
"""
r_LMPValFunc = robjects.r(PStr)

Best,
Collin.


On Tue, 29 Oct 2013, Erin Hodgess wrote:

> Hello again!
>
> I'm using python with a module rpy2 to call functions from R.
>
> It works fine on built in R functions like rnorm.
>
> However, I would like to access user-defined functions as well.  For those
> of you who use this, I have:
>
> import rpy2.robjects as R
> R object as no attribute buzz
>
> (user defined function of buzz)
>
> This is on a Centos 5 machine with R-3.0.2 and python of 2.7.5.
>
> Thanks for any help.
> Sincerely,
> Erin
>
>
>
> --
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodg...@gmail.com
>
>   [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] rpy2 and user defined functions from R

2013-10-30 Thread R. Michael Weylandt
Could you produce a full working example then? Bit hard to debug without 
knowing what you did. 

Michael

On Oct 30, 2013, at 9:11, Erin Hodgess  wrote:

> I did...just didn't show it
> 
> 
> On Wed, Oct 30, 2013 at 7:01 AM, Michael Weylandt 
>  wrote:
>> Presumably you need to define 'buzz' first, but I don't see evidence that 
>> you've done so.
>> 
>> Michael
>> 
>> On Oct 30, 2013, at 0:06, Erin Hodgess  wrote:
>> 
>> > Hello again!
>> >
>> > I'm using python with a module rpy2 to call functions from R.
>> >
>> > It works fine on built in R functions like rnorm.
>> >
>> > However, I would like to access user-defined functions as well.  For those
>> > of you who use this, I have:
>> >
>> > import rpy2.robjects as R
>> > x = R.r.buzz(3)
>> > R object as no attribute buzz
>> >
>> > (user defined function of buzz)
>> >
>> > This is on a Centos 5 machine with R-3.0.2 and python of 2.7.5.
>> >
>> > Thanks for any help.
>> > Sincerely,
>> > Erin
>> >
>> >
>> >
>> > --
>> > Erin Hodgess
>> > Associate Professor
>> > Department of Computer and Mathematical Sciences
>> > University of Houston - Downtown
>> > mailto: erinm.hodg...@gmail.com
>> >
>> >[[alternative HTML version deleted]]
>> >
>> > __
>> > R-help@r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide 
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 
> -- 
> Erin Hodgess
> Associate Professor 
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodg...@gmail.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] rpy2 and user defined functions from R

2013-10-30 Thread Erin Hodgess
I did...just didn't show it


On Wed, Oct 30, 2013 at 7:01 AM, Michael Weylandt <
michael.weyla...@gmail.com> wrote:

> Presumably you need to define 'buzz' first, but I don't see evidence that
> you've done so.
>
> Michael
>
> On Oct 30, 2013, at 0:06, Erin Hodgess  wrote:
>
> > Hello again!
> >
> > I'm using python with a module rpy2 to call functions from R.
> >
> > It works fine on built in R functions like rnorm.
> >
> > However, I would like to access user-defined functions as well.  For
> those
> > of you who use this, I have:
> >
> > import rpy2.robjects as R
> > x = R.r.buzz(3)
> > R object as no attribute buzz
> >
> > (user defined function of buzz)
> >
> > This is on a Centos 5 machine with R-3.0.2 and python of 2.7.5.
> >
> > Thanks for any help.
> > Sincerely,
> > Erin
> >
> >
> >
> > --
> > Erin Hodgess
> > Associate Professor
> > Department of Computer and Mathematical Sciences
> > University of Houston - Downtown
> > mailto: erinm.hodg...@gmail.com
> >
> >[[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>



-- 
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: erinm.hodg...@gmail.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] rpy2 and user defined functions from R

2013-10-30 Thread Michael Weylandt
Presumably you need to define 'buzz' first, but I don't see evidence that 
you've done so. 

Michael

On Oct 30, 2013, at 0:06, Erin Hodgess  wrote:

> Hello again!
> 
> I'm using python with a module rpy2 to call functions from R.
> 
> It works fine on built in R functions like rnorm.
> 
> However, I would like to access user-defined functions as well.  For those
> of you who use this, I have:
> 
> import rpy2.robjects as R
> x = R.r.buzz(3)
> R object as no attribute buzz
> 
> (user defined function of buzz)
> 
> This is on a Centos 5 machine with R-3.0.2 and python of 2.7.5.
> 
> Thanks for any help.
> Sincerely,
> Erin
> 
> 
> 
> -- 
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodg...@gmail.com
> 
>[[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.