0

I use RStudio Workbench. When I create a simple function like that:

sample <- function() {
    df <- data.frame('a' = c(1:10), 'b' = c(1:10))
    dt <- data.table::as.data.table(df)

    return (dt[, .(b)])
}

all is fine. When I call this with sample(), there is no error. The package "data.table" is available in the system library. I didn't load it with library().

When I use "devtools" to create a package. Then I get the error: Error in .(b) : could not find function "."

Here is the flow to reproduce the error:

Create the package directory with:

devtools::create('home/development/sample', rstudio = FALSE)

Save the file "sample.R" in the R directory.

#' Example
#'
#' Where is the problem?
#'
#' @return Column 'b' from a data table.
#'
#' @examples
#' \code{sample()}
#'
#' @export
sample <- function() {
    df <- data.frame('a' = c(1:10), 'b' = c(1:10))
    dt <- data.table::as.data.table(df)

    return (dt[, .(b)])
}

Document the package and load it.

devtools::document('home/development/sample')
devtools::load_all('home/development/sample', export_all = FALSE, reset = TRUE)
sample::sample()
# Console: Error in .(b) : could not find function "." 

Where could be the problem? Is anything to do with devtools? E.g. with the DESCRIPTION file? I use data.table with data.table::name() and he used in the global environment the .() syntax. But not with the package? Where I have to load it? Thanks for your help in advance.

Phil
  • 7,287
  • 3
  • 36
  • 66
Pascal
  • 43
  • 5
  • 1
    Possible duplicate of https://stackoverflow.com/questions/55906277/import-from-data-table-so-that-lintr-recognizes-it – akrun Nov 08 '22 at 17:45
  • The code is using `[.data.table` but because `library(data.table)` was not used it cannot find it. – G. Grothendieck Nov 08 '22 at 17:46
  • 2
    You could add in roxygen comments :`#' @import data.table` – Waldi Nov 08 '22 at 18:00
  • 1
    See also: https://cran.r-project.org/web/packages/data.table/vignettes/datatable-importing.html – MrFlick Nov 08 '22 at 18:34
  • @G.Grothendieck When I call "(.packages())", data.table is not loaded. But my first code (not in package) runs. Simple in the global environment. Without loading data.table. Why is this possible? And when I load it with library(data.table), the code in the package still doesn't run. – Pascal Nov 08 '22 at 18:49
  • @Waldi I try it again and now, it runs. Thanks for the answer. I forgot to renew the NAMESPACE in package. – Pascal Nov 08 '22 at 19:12
  • It seems I was wrong about `[.data.table` . Simply using `data.table::as.data.table` without using `library` will still load additional methods. – G. Grothendieck Nov 08 '22 at 19:14
  • That is possible. I'm new in R, so every comment or hint helps me :-) – Pascal Nov 08 '22 at 19:17
  • 1
    This error may just be due to package checking so if we define a dummy dot function that may get around it. Alternately use `list` instead of dot. – G. Grothendieck Nov 08 '22 at 19:24

0 Answers0