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)
}