0

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?

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • At a guess, have you tried using `out <- x[, eval(exp, envir = environment()),`? It seems like there's an issue with scoping. – paleo13 May 12 '17 at 00:58
  • 1
    When you "run interactively", you are loading data.table and so have access to `[.data.table`, but when you run it in your own package, you are not loading/importing data.table, so instead you get `[.data.frame`... at least that seems plausible to me, as someone who hasn't made any packages. – Frank May 12 '17 at 02:12
  • 2
    Try adding `require(data.table)` in the first line of your function. – Yannis Vassiliadis May 12 '17 at 02:26
  • 1
    Inside the functions in a package, it is better not to use `library(data.table)` or `require(data.table)` – akrun May 12 '17 at 04:02
  • Should we close this as a typo / not reading the FAQ https://rawgit.com/wiki/Rdatatable/data.table/vignettes/datatable-faq.html#i-have-created-a-package-that-depends-on-data.table.-how-do-i-ensure-my-package-is-data.table-aware-so-that-inheritance-from-data.frame-works ? "Either i) include data.table in the Depends: field of your DESCRIPTION file, or ii) include data.table in the Imports: field of your DESCRIPTION file AND import(data.table) in your NAMESPACE file." You have it in Imports but have not added it to the NAMESPACE... – Frank May 12 '17 at 16:35
  • @Frank You nailed it. no on close as that's the answer. This is in no way a typo. Typo is a misspelling. There is no `not reading the FAQ` for a reason to vote to close. Both of these suggestions are inappropriate. I think the appropriate move forward is to move your comment into an answer. – Tyler Rinker May 12 '17 at 17:08
  • Ok, will do re answering. I tend to broaden that close reason to "this was misapprehension or misapplication of the documented usage", but I'm fine with leaving it open too. Just looked like you'd abandoned it. – Frank May 12 '17 at 19:04
  • 1
    Definitely a dupe – Tyler Rinker May 12 '17 at 19:41

1 Answers1

1

This may be due to the fact that we are not specifying it in the 'Depends' in 'DESCRIPTION'

enter image description here

The package was built, installed and was able to run the example

enter image description here

NOTE: Author:, Maintainer: fields are not empty in the 'DESCRIPTION' file

NOTE2: Used the same function combine_tot with no additional changes

akrun
  • 874,273
  • 37
  • 540
  • 662