I am attempting to use data.table inside a function in a package.
combine_tot <- function(x){
nms <- colnames(x)
colnames(x) <- c('person', 'texts')
x <- data.table::data.table(x)
exp <- parse(text='list(text = paste(texts, collapse = " "))')[[1]]
out <- x[, eval(exp),
by = list(person, 'new' = data.table::rleid(person))][,
'new' := NULL][]
data.table::setnames(out, nms)
out
}
Here is a dummy package demonstrating this: https://github.com/trinker/testdt
If I'm interactive the function works fine. If I use it in a package then I get the following error:
Error in paste(texts, collapse = " ") : object 'texts' not found
But if I run it interactively I get the following as expected:
library(data.table)
x <- data.frame(a= c('a', 'a', 's', 'f', 'd', 'a', 's', 's'), b = state.name[1:8])
combine_tot(x)
a b
1: a Alabama Alaska
2: s Arizona
3: f Arkansas
4: d California
5: a Colorado
6: s Connecticut Delaware
I've have used data.table in other similar circumstances using eval(parse
but I can not make this work. What am I doing wrong?