2

I have the following function which works only for quoted variables:

library(data.table) # version 1.11.8
library(purrr)

col_count <- function(dt, vars = NULL){
  dt[, .N, by = vars]
}

I have successfully created basically the same function which is able to receive both quoted and unquoted variables (thanks to the 'by' argument in data.table which receives a list or a character vector).

col_count2 <- function(dt, ...){
  vars <- NULL
  try(if(is.character(...)) {vars <- unlist(eval(substitute(...)))}, silent = TRUE)

  if(is.null(vars)) vars <- substitute(list(...))

  dt[, .N, by = vars]
}
dt_iris <- data.table::as.data.table(iris)

identical(col_count2(dt_iris, Petal.Width, Species), 
          col_count2(dt_iris, c('Petal.Width', 'Species')))
[1] TRUE

Now I want my function to be able to be iterated with purrr::map (or lapply) as follows:

purrr::map(colnames(dt_iris), ~ col_count(dt_iris, .))

which works.

But

purrr::map(colnames(dt_iris), ~ col_count2(dt_iris, .))
Error in eval(bysub, x, parent.frame()) : object '.' not found 

This is a message from data.table, which receives '.' as the vars variable in the 'by =' expression. purrr::map sends '.' and as this is not a character it goes directly to:

vars <- substitute(list(...))

I'm guessing there's a better way to solve the quote/unquote problem and stay within data.table.

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
Telaroz
  • 173
  • 1
  • 7

1 Answers1

3

We can use :

library(data.table)

col_count2 <- function(dt, ...){
   vars <- NULL 
   tryCatch({vars <- list(...)[[1]]}, error = function(e) {})
   if(!is.character(vars)) vars <- as.character(substitute(list(...))[-1])
   dt[, .N, by = vars]
}

identical(col_count2(dt_iris, Petal.Width, Species), 
          col_count2(dt_iris, c('Petal.Width', 'Species')))
#[1] TRUE

purrr::map(colnames(dt_iris), col_count2, dt = dt_iris)

#[[1]]
#    Sepal.Length  N
# 1:          5.1  9
# 2:          4.9  6
# 3:          4.7  2
# 4:          4.6  4
#....

#[[2]]
#    Sepal.Width  N
# 1:         3.5  6
# 2:         3.0 26
# 3:         3.2 13
# 4:         3.1 11
#....
#....
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks! I see that the [-1] removes the first element in the call (list) so we only keep the colnames. How can I see the complete structure of substitute(list(...))? str doesn't show it. – Telaroz May 02 '20 at 16:05
  • 1
    you can add `browser()` in the function so you can debug and see what actual contents are. Or just `print(vars)`. – Ronak Shah May 02 '20 at 16:32