[datatable-help] eval(eval issue

Matthew Dowle mdowle at mdowle.plus.com
Sun Aug 14 12:02:35 CEST 2011


Also, 'by' can be a character vector of column names.

> DT = data.table(a=1:2,b=1:3,c=1:12)
> DT
      a b  c
 [1,] 1 1  1
 [2,] 2 2  2
 [3,] 1 3  3
 [4,] 2 1  4
 [5,] 1 2  5
 [6,] 2 3  6
 [7,] 1 1  7
 [8,] 2 2  8
 [9,] 1 3  9
[10,] 2 1 10
[11,] 1 2 11
[12,] 2 3 12
> myby = c("a")
> DT[,sum(c),by=myby]
     a V1
[1,] 1 36
[2,] 2 42
> myby = c("a","b")
> DT[,sum(c),by=myby]
     a b V1
[1,] 1 1  8
[2,] 2 2 10
[3,] 1 3 12
[4,] 2 1 14
[5,] 1 2 16
[6,] 2 3 18

In your case since the byExpr isn't really an 'expression' of column
names (in the sense of calling a function such as month() on a date
column), it can simplify to :

ByCols = c("STATE", "CONTENT_AREA", "YEAR",
"GRADE","STATE_ENROLLMENT_STATUS", "CATCH_UP_KEEP_UP_STATUS_INITIAL")

tmp <- tmp.dt[, eval(ListExpr), by=ByCols]

Matthew


On Sun, 2011-08-14 at 10:48 +0100, Matthew Dowle wrote:
> Not sure why there's an extra level of quote() inside the string it's
> parsing; i.e., instead of :
> 
> ListExpr <- parse(text=paste("quote(as.list(c(",
> paste(unlist(tmp.sgp.summaries), collapse=", "),")))",sep=""))       
> 
> ByExpr <- parse(text=paste("quote(list(", paste(sgp.groups.to.summarize,
> collapse=", "), "))", sep=""))
> 
> tmp <- tmp.dt[, eval(eval(ListExpr)), by=eval(eval(ByExpr))]
> 
> 
> try removing the quote() as follows (also changing the as.list to list
> to make a considerable speed improvement as per wiki example 4) :
> 
> ListExpr <- parse(text=paste("list(", paste(unlist(tmp.sgp.summaries),
> collapse=", "),")",sep=""))       
> 
> ByExpr <- parse(text=paste("list(", paste(sgp.groups.to.summarize,
> collapse=", "), "))", sep="")
> 
> tmp <- tmp.dt[, eval(ListExpr), by=eval(ByExpr)]
> 
> 
> If that doesn't fix it then we'll need a reproducible example please
> that we can paste into R. Should be possible to create one in this case
> as it's a type error. Also a confirm that you're using v.1.6.4 and which
> version of R please as this kind of error can be affected by that.
> 
> Matthew
> 
> 
> On Sat, 2011-08-13 at 20:49 -0500, Damian Betebenner wrote:
> > As an update. The issue that seems to be throwing the error is the
> > call to a function “percent_in_category” that has single quoted
> > expressions in the ListExpr:
> > 
> >  
> > 
> > Browse[1]> ListExpr
> > 
> > expression(quote(as.list(c(median_na(SGP), median_na(SGP_TARGET),
> > percent_in_category(CATCH_UP_KEEP_UP_STATUS, list(c('Catch Up: Yes',
> > 'Keep Up: Yes')), list(c('Catch Up: Yes', 'Catch Up: No', 'Keep Up:
> > Yes', 'Keep Up: No'))), num_non_missing(SGP),
> > percent_in_category(ACHIEVEMENT_LEVEL, list(c('Proficient',
> > 'Advanced')), list(c('Unsatisfactory', 'Partially Proficient',
> > 'Proficient', 'Advanced'))), num_non_missing(ACHIEVEMENT_LEVEL),
> > percent_in_category(ACHIEVEMENT_LEVEL_PRIOR, list(c('Proficient',
> > 'Advanced')), list(c('Unsatisfactory', 'Partially Proficient',
> > 'Proficient', 'Advanced'))),
> > num_non_missing(ACHIEVEMENT_LEVEL_PRIOR)))))
> > 
> >  
> > 
> > When I isolate in my testing, it gives the same error:
> > 
> >  
> > 
> > Error during wrapup: invalid 'type' (character) of argument
> > 
> >  
> > 
> >  
> > 
> > Anybody know of a workaround for this or a simplification of the
> > approach I’m taking.
> > 
> >  
> > 
> > thanks,
> > 
> >  
> > 
> > Damian
> > 
> >  
> > 
> >  
> > 
> >  
> > 
> > Damian Betebenner
> > 
> > Center for Assessment
> > 
> > PO Box 351
> > 
> > Dover, NH   03821-0351
> > 
> >  
> > 
> > Phone (office): (603) 516-7900
> > 
> > Phone (cell): (857) 234-2474
> > 
> > Fax: (603) 516-7910
> > 
> >  
> > 
> > dbetebenner at nciea.org
> > 
> > www.nciea.org
> > 
> >  
> > 
> >  
> > 
> > 
> >  
> > 
> > From: Damian Betebenner 
> > Sent: Saturday, August 13, 2011 9:05 PM
> > To: 'datatable-help at lists.r-forge.r-project.org'
> > Subject: eval(eval issue
> > 
> > 
> >  
> > 
> > All,
> > 
> >  
> > 
> > A line of code that used to work version 1.6.2 is now throwing an
> > error so I thought I’d see if anyone knows exactly what is going on.
> > The error that I now get is:
> > 
> >  
> > 
> > Error during wrapup: invalid 'type' (character) of argument
> > 
> >  
> > 
> > The call that is generating the error is:
> > 
> >  
> > 
> > tmp <- tmp.dt[, eval(eval(ListExpr)), by=eval(eval(ByExpr))]
> > 
> >  
> > 
> >  
> > 
> > It is essentially a 
> > 
> >  
> > 
> > x[,j=ListExpr,by=ByExpr] call but the j is a long of custom functions
> > and the ByExpr is a list of variables in x that changes via a loop.
> > 
> >  
> > 
> > Currently (what is broken), the ListExpr and ByExpr are created using
> > the following:
> > 
> >  
> > 
> >       ListExpr <- parse(text=paste("quote(as.list(c(",
> > paste(unlist(tmp.sgp.summaries), collapse=", "),")))",sep=""))       
> > 
> >       ByExpr <- parse(text=paste("quote(list(",
> > paste(sgp.groups.to.summarize, collapse=", "), "))", sep=""))
> > 
> >  
> > 
> > For example, ListExpr is:
> > 
> >  
> > 
> > Browse[1]> ListExpr
> > 
> > expression(quote(as.list(c(median_na(SGP), median_na(SGP_TARGET),
> > percent_in_category(CATCH_UP_KEEP_UP_STATUS, list(c('Catch Up: Yes',
> > 'Keep Up: Yes')), list(c('Catch Up: Yes', 'Catch Up: No', 'Keep Up:
> > Yes', 'Keep Up: No'))), num_non_missing(SGP),
> > percent_in_category(ACHIEVEMENT_LEVEL, list(c('Proficient',
> > 'Advanced')), list(c('Unsatisfactory', 'Partially Proficient',
> > 'Proficient', 'Advanced'))), num_non_missing(ACHIEVEMENT_LEVEL),
> > percent_in_category(ACHIEVEMENT_LEVEL_PRIOR, list(c('Proficient',
> > 'Advanced')), list(c('Unsatisfactory', 'Partially Proficient',
> > 'Proficient', 'Advanced'))),
> > num_non_missing(ACHIEVEMENT_LEVEL_PRIOR)))))
> > 
> >  
> > 
> >  
> > 
> > And ByExpr is:
> > 
> >  
> > 
> > Browse[1]> ByExpr
> > 
> > expression(quote(list(STATE, CONTENT_AREA, YEAR, GRADE,
> > STATE_ENROLLMENT_STATUS, CATCH_UP_KEEP_UP_STATUS_INITIAL)))
> > 
> >  
> > 
> >  
> > 
> > I’m sure this can be simplified.
> > 
> >  
> > 
> > Any help greatly appreciated,
> > 
> >  
> > 
> > Damian
> > 
> >  
> > 
> >  
> > 
> >  
> > 
> >  
> > 
> > Damian Betebenner
> > 
> > Center for Assessment
> > 
> > PO Box 351
> > 
> > Dover, NH   03821-0351
> > 
> >  
> > 
> > Phone (office): (603) 516-7900
> > 
> > Phone (cell): (857) 234-2474
> > 
> > Fax: (603) 516-7910
> > 
> >  
> > 
> > dbetebenner at nciea.org
> > 
> > www.nciea.org
> > 
> >  
> > 
> >  
> > 
> >  
> > 
> > 
> > _______________________________________________
> > 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