16

I have a function to remove empty columns from a data.table, and included that in a package.

Somehow it works when I load the function, but not when I call it from the package. Question: why doesn't this function run when I call it from a package?

There is no require(data.table) or library(data.table) in any of the functions in the package. DESCRIPTION file contains: Imports: data.table. So Using data.table package inside my own package is satisfied.

library(data.table)
df = data.table(a = c(1,2,3), b = c(NA, NA, NA), c = c(4,5,6))
library(cr360)

remove.emptycols(df) # from package
Error in .subset(x, j) : invalid subscript type 'list'

# now open function from mypackage and run again:
# source("./mypackage/R/fun_remove_emptycols.R")
remove.emptycols(df)
   a c
1: 1 4
2: 2 5
3: 3 6

the function:

#' Remove empty columns
#' 
#' Counts the number of NA values in the columns and counts the number of rows.
#' @param df
#' @return df data.table with empty columns removed.
#' @export
#' 
#' 
remove.emptycols = function(df) {

count.colNA = df[,lapply(.SD, function(x) sum(is.na(x)))] 
df = df[,which(count.colNA != nrow(df)),with = FALSE]  

return(df)
}
Community
  • 1
  • 1
Henk
  • 3,634
  • 5
  • 28
  • 54
  • Strange. Have you rebuilt your package with the new DESCRIPTION file and reinstalled your package? – Matt Dowle Jun 24 '14 at 09:17
  • I did. Also tried the modules package [more python like module approach alternative for packages], same result. Built and reloaded package. Build and restarted R: same result. Am now just loading the functions in a new namespace: utils = new.env(), source(utils$remove.emptycols = function ....That works fine. – Henk Jun 24 '14 at 09:29
  • 1
    Oh. Please paste result of `sessionInfo()`, `packageVersion("data.table")` and does your package have a NAMESPACE file? – Matt Dowle Jun 24 '14 at 09:51
  • Long output, so here are the files: https://copy.com/4hN8Mm1LyrpH https://copy.com/q7SQFqLqjfhm – Henk Jun 24 '14 at 10:29
  • There appears to be a line break: `Imports: \n data.table`. Remove that so the field is one line, rebuild, reinstall and retry. – Matt Dowle Jun 24 '14 at 11:16
  • And if it isn't that, does your script `get_data.R` contain a `require(data.table)`? Would be useful to see that script. – Matt Dowle Jun 24 '14 at 11:53
  • re DESCRIPTION file line break - I saved it with a normal space, but after building the package it comes out with a line break. The get_data.R script contained a require(data.table), but after removal it complains 'could not find function "data.table" '. Link to get_data.R : https://copy.com/X3ZpSjnRmYIa. – Henk Jun 24 '14 at 12:18

1 Answers1

14

The text

import(data.table)

needs to be in the NAMESPACE file as well as data.table being in the Imports: field in the DESCRIPTION field. I've edited the linked question and updated FAQ 6.9.
Using data.table package inside my own package

Also, in RStudio be aware of the option "Use Roxygen to build NAMESPACE file" and see:
Does roxygen2 automatically write NAMESPACE directives for "Imports:" packages?


Previous red herring kept for posterity ...

Not sure, but your package's DESCRIPTION contained :

...
Version: 1.0
Date: 2014-06-23
Imports:
    data.table
Author: Henk
Description: utility functions
...

Try removing the line break and this instead :

...
Version: 1.0
Date: 2014-06-23
Imports: data.table
Author: Henk
Description: utility functions
...
Community
  • 1
  • 1
Matt Dowle
  • 58,872
  • 22
  • 166
  • 224
  • Tried that, somehow it comes back with the line break after building the package. So I save exactly as the second code block, then after package build it comes out exactly in the first code block. – Henk Jun 24 '14 at 12:30
  • Got it. The whole thing works if I replace "Imports: data.table" with "Depends: data.table". It still saves with the line break, but the functions work as intended. Thanks for pointing me in the right direction! – Henk Jun 24 '14 at 13:51
  • Great but you shouldn't have to Depend, should be fine with Imports which other packages do ok. Strange! Perhaps try `Imports: methods, data.table` just to i) see if it's an edge case when just 1 package is imported but also ii) make sure that the reinstall is actually working. I've sometimes made "no effect" inferences before when in fact another R process is blocking or not restarting a new xterm, or zombie process means that the previously installed package files are still loaded. – Matt Dowle Jun 24 '14 at 14:08
  • Now I suddenly had the error message that no DESCRIPTION file could be found, whereas it was there, in front of my eyes, in the right directory. I have migrated all scripts to a new package project, and everything works. Nevertheless, this has cost me so much time that I just load the scripts into a new environment rather than go the package way. It's a Heisenbug generator! – Henk Jun 24 '14 at 14:25
  • It works...but no idea whether this is because of imports, depends, a new description file, or starting up a new package development project. – Henk Jun 24 '14 at 15:06
  • last/final update: only "Depends: data.table" works. Using either "Imports: methods, data.table" or "Imports: data.table" results in the same error message. – Henk Jun 24 '14 at 15:51
  • 1
    One last shot ... according to [this](http://stackoverflow.com/a/8028648/403310) you need `import(data.table)` in NAMESPACE as well as `Imports: data.table` in DESCRIPTION. Your NAMESPACE file looks to be missing that. I checked a selection of 10 CRAN packages that Import: data.table and they all have `import(data.table)` in their NAMESPACE file as well. If that is it then I'll update the FAQ and linked question. Apologies. – Matt Dowle Jun 24 '14 at 17:51
  • Tried to include import(data.table) in NAMESPACE, but R removes this during the compilation of the package. It really only works with Depends: data.table in DESCRIPTION, with R replacing a space with a line break during compilation. – Henk Jun 25 '14 at 07:37
  • How exactly are you reaching the conclusion that R removes the import(data.table) line from NAMESPACE file during compilation? Are you sure you're looking at the files in the right place? There are 32 CRAN packages that import data.table and that doesn't happen for them. I've looked at the tar.gz NAMESPACE file, and the installed NAMESPACE file in various packages and R doesn't removed any lines from those. How are you "compiling" - you mean `R CMD build`? – Matt Dowle Jun 25 '14 at 09:58
  • Yes, I should have been more precise. Procedure is like this: I open NAMESPACE inside the project directory in RStudio and add: import(data.table). Next, I do "Build & Reload" in the Build tab. When I open that same NAMESPACE again, the import(data.table) is no longer there. Indeed, compile = "R CMD INSTALL --no-multiarch --with-keep.source cr360". – Henk Jun 25 '14 at 13:41
  • I've asked in R chat and @Roland asks are you "using roxgen2 to create the NAMESPACE file? That's an option in RStudio." – Matt Dowle Jun 25 '14 at 14:46
  • 2
    yes indeed - everything is done with the devtools/roxygen. I have switched off the option: "Use Roxygen to build NAMESPACE file" under configuration. Now if I add import(data.table) in NAMESPACE it remains there. If I have Imports: data.table in DESCRIPTION and import: data.table in NAMESPACE everything works. Conclusion so far: either Depends: data.table in DESCRIPTION, *or* switch off roxygen for NAMESPACE and include Imports:data.table in DESCRIPTION *and* import(data.table) in NAMESPACE. Getting there! Pfff... – Henk Jun 25 '14 at 17:10
  • Relief! Thanks to @Roland. – Matt Dowle Jun 25 '14 at 17:48