[datatable-help] Environment of eval() execution for "j" appears to vary inexplicably

Matthew Dowle mdowle at mdowle.plus.com
Tue Jun 29 22:43:26 CEST 2010


Yes, that was reproducible, thanks.

The last commit 101 fixes this one too, I think. Please confirm.

A = data.table(a=c("A","C","D"), Count=c(4L,8L,1L))

> foo1(A)
[1] TRUE
[1] A C
Levels: A C D
[1] A C
Levels: A C D

> foo2(A,a)
[1] TRUE
[1] A C
Levels: A C D
[1] A C
Levels: A C D
[1] A C D
Levels: A C D (Other)
> 

Matthew


On Sat, 2010-06-26 at 00:28 -0700, Harish wrote:
> I am running into a peculiar issue which seems to be related to the environment in which the eval() is executed when passed as the "j".  The environment of execution of the eval() seems to vary depending on whether I pass in a variable (of class "name") or an equivalent expression is typed inside the eval.
> 
> === Example code ===
> 
> A <- structure(list(a = structure(1:3, .Label = c("A", "C", "D"), class = "factor"), 
>     Count = c(4L, 8L, 1L)), .Names = c("a", "Count"), class = "data.table")
> 
> foo1 <- function(DT) {
>    dtRet <- DT[ ,
>             list( Count=sum( Count ) ),
>             by=list( Category=foo2( DT, a ) )
>          ]
>    invisible()
> }
> 
> 
> foo2 <- function( DT, v ) {
>    q <- substitute( v )
> 
>    print( identical( q, substitute( v ) ) )   # TRUE as expected
>    print( DT[ 1:2, eval( q ) ] )
>    print( DT[ 1:2, eval( substitute( v ) ) ] )
>    return( structure(1:3, .Label = c("A", "C", "D", "(Other)"), class = "factor") )
> }
> 
> foo1( A ) # Test 1
> foo2( A, a ) # Test 2
> === End code ===
> 
> In Test 1, when I run foo1(), I am essentially executing
>    foo2( A, a ) from within the code of the data table.
> 
> I get:
> [1] TRUE
> [1] A C
> Levels: A C D
> [1] A C D
> Levels: A C D
> 
> Issue #1 ==> The third print in foo2() is actually returning 3 items when I am requesting only the first 2 items.  (Also, in my more complex program, it seemed to return the data in alphabetical order or the order of the factor levels rather than in the order of the data in the table.  However, I am not able to reproduce this in a simpler example.  I am hoping that this behavior will also be rectified with any bug fixes you make.)
> 
> In Test 2, I run foo2() directly in .GlobalEnv, but I am passing in the same data that foo1() would have passed it in Test 1.
> 
> I get:
> [1] TRUE
> [1] A C
> Levels: A C D
> Error in eval(expr, envir, enclos) : object 'a' not found
> 
> Issue #2 ==> It looks like if I have an expression inside eval(), it is executed in a different environment as the prior print statement where I have an eval() with just a single variable.  Technically, I would expect both to be equivalent.
> 
> 
> I hope I clearly explained what my issues are.
> 
> 
> Regards,
> Harish
> 
> 
> 
>       
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help




More information about the datatable-help mailing list