I see.

In R's `transform()` there is the following line:
     e <- eval(substitute(list(...)), `_data`, parent.frame())
(`_data` being the data.frame)
This will work with R's `language` elements, but unfortunately not 
`expression` ones (which what is coming out out of `parse()`).

I'd need (or someone would need to contribute) a way to handle 
`language` objects from rpy2 simply (there might be a way to do it as 
things are currently, but I suspect more trouble than it is worth).

In the meantime, moving this sort of operation to Python is straightforward:

     def transform(df, **kwargs):
         """ Modify in-place the columns of an R DataFrame """
         for k in kwargs:
             i = df.names.index(k)
             df[i] = kwargs[k](df[i])


     from rpy2.robjects.vectors import DataFrame, IntVector
     dataf = DataFrame({'a': IntVector((1,2,3))})

     transform(dataf, a = lambda x: x.ro + 1)


The other alternative is to evaluate the full line as R code. The 
DataFrame does not have to be interpolated in the line, it can passed to 
an R environement, and the code evaluation made in that environment (as 
an alternative to having everything in the R "global environment").

L.

On 2013-04-27 10:57, Carlos Pita wrote:
> Hi Laurent,
>
> no it doesn't. Indeed I had tried with parse before asking but to no
> avail. My code was:
>
> r.transform(r['data.frame'](a=r.c(1,2,3)), a=parse('a+1'))
>
> Whose output is the same than your code's:
>
> <DataFrame - Python:0xa78e84c / R:0xa302a60>
> [Vector]
>    a: <class 'rpy2.robjects.vectors.Vector'>
>    <Vector - Python:0xa7926ac / R:0xa6ffbc0>
> [Vector, Vector, Vector]
>
> Regards,
> Carlos
>
> On Sat, Apr 27, 2013 at 5:40 AM, Laurent Gautier <lgaut...@gmail.com> wrote:
>> Hi,
>>
>> Would the following help ?
>>
>>      from rpy2.rinterface import parse
>>      from rpy2.robjects.vectors import DataFrame, IntVector
>>      from rpy2.robjects.packages import importr
>>      base = importr('base')
>>      base.transform(DataFrame({'a': IntVector((1,2,3))}),
>>             a = parse('a+1'))
>>
>> Best,
>>
>>
>> L.
>>
>>
>>
> Your snippet returns:
>
> Out[10]:
> <DataFrame - Python:0xa7e7fac / R:0xa2b4a50>
> [Vector]
>    a: <class 'rpy2.robjects.vectors.Vector'>
>    <Vector - Python:0xa7e8eec / R:0x9a86648>
> [Vector, Vector, Vector]
>
> Guess each number in the original vector is just being replaced by an
> expression.
>> On 2013-04-27 09:23, Carlos Pita wrote:
>>> Hi all,
>>>
>>> I would like to know if there is a way to pass an r expression to be
>>> lazy evaluated by a function. I mean as in transform or within. For
>>> example:
>>>
>>>    r.transform(r['data.frame'](a=r.c(1,2,3)), a=<<a+1>>)
>>>
>>> The part <<a+1>> is the one I want to achieve somehow. I know I could just
>>> call:
>>>
>>>    r('transform(data.frame(a=c(1,2,3)), a=a+1)')
>>>
>>> But this requires to pass the data frame inline which in this case is
>>> ok because of its tiny size, but it's not the general case. Also, I
>>> could add the data frame to the environment variable x and then call:
>>>
>>> r('transform(x, a=a+1)')
>>>
>>> But as I've already said I would like to know if there is some way to
>>> pass the a+1 expression as an object.
>>>
>>> Best regards
>>> --
>>> Carlos
>>>
>>>
>>> ------------------------------------------------------------------------------
>>> Try New Relic Now & We'll Send You this Cool Shirt
>>> New Relic is the only SaaS-based application performance monitoring
>>> service
>>> that delivers powerful full stack analytics. Optimize and monitor your
>>> browser, app, & servers with just a few lines of code. Try New Relic
>>> and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
>>> _______________________________________________
>>> rpy-list mailing list
>>> rpy-list@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/rpy-list
>>


------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service 
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to