[datatable-help] Fail to add new columns within a function
Huashan Chen
chenhuashan at gmail.com
Wed Dec 18 10:04:29 CET 2013
OK, here is the complete code with some mock functions from my example.
# data: data.table object
# fn: a filename to read data from
merge_data<-function(fn, data){
fs<-getSavedata(fn) # read as data.frame
if (is.null(fs)) stop('Empty data file')
# return a character vector of variable names which are to merged, some
variables in fs will not be merged to DT
newvars<-selectVars(names(fs))
stopifnot(length(newvars) > 0)
# determine which rows to use
caseid<-someCustomFunc(fs)
add_var<-function(varname){
data[caseid, `:=`(eval(substitute(varname)), fs[,
toupper(eval(substitute(varname)))]), with=F]
}
invisible(sapply(newvars, add_var))
}
# calling function
merge_data('some file', DT)
DT # display the updated results
In this case, I think a warning from merge_data() when the quota is reached
would be appreciated. Of couse, I could have added a check within the
function to avoid unintended action.
if (truelength(data) <= ncol(data) + 64L) stop('increase colunmn quota
using alloc.col() before calling this function.')
--
View this message in context: http://r.789695.n4.nabble.com/Fail-to-add-new-columns-within-a-function-tp4682173p4682393.html
Sent from the datatable-help mailing list archive at Nabble.com.
More information about the datatable-help
mailing list