r - passing correct environment to nested function -


in code, use function simple thing: takes records selected given condition , adds give text "errors" column:

dterr <- function (dtin, condition, errtext) {   if (!is.element('errors', names(dtin))) {dtin[,errors:=""];}   dtin[eval(substitute(condition)), errors:={paste0(errors, errtext)}];   invisible(dtin); } 

(i thankful people helped previous question eval(substitute))

so, in simple cases function works expected:

dt1 <- fread(   "id,cola,colb    id1,3,xxx    id2,0,zzz    id3,na,yyy    id4,0,aaa      ")  mynum=0  dterr(dt1, cola>mynum, "positive"); dt1 #    id   cola colb   errors # 1: id1    3  xxx   positive # 2: id2    0  zzz          # 3: id3   na  yyy          # 4: id4    0  aaa   

but when try call function, in combination variable defined inside, error:

myfun <- function(){   mynum2=1;   dterr(dt1, cola>mynum2, "big!"); }  myfun() # error in eval(expr, envir, enclos) : object 'mynum2' not found  

obviously, should modify function pass correct environment eval - moment can't find correct solution (i played different combinations of parent.frame(), environment(), etc). hints appreciated.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -