0

I am currently developing a package in which I try to implement a function that internally uses dplyr::group_by. However, upon package checking (devtools CRAN checks), I get a NOTE stating that no visible binding for global variable Levels.

foo <- function(x) {
  out <- data.frame(Levels = x) %>%
    group_by(Levels) %>%
    summarise(n = n())
   return(out)
}
foo: no visible binding for global variable 'Levels'
  Undefined global functions or variables:
    Levels

In order to do it in a non-NSE fashion, I could use group_by_() with a string, but I understood that this way of doing this is deprecated, in favour of the use of quosures. However, I still have trouble figuring out how to do it.

I tried using one_of(), without success:

foo <- function(x) {
  table_full <- data.frame(Levels = x) %>%
    group_by(one_of("Levels")) %>%
    summarise(n = n())
}
Error in mutate_impl(.data, dots) : 
  Evaluation error: No tidyselect variables were registered

What would be the most correct way of achieving that?

Dominique Makowski
  • 1,511
  • 1
  • 13
  • 30

1 Answers1

0

Am I missing something or couldn't you do:

library(dplyr)

foo <- function(x) {
  data.frame(Levels = x) %>%
    group_by(Levels) %>%
    summarise(n = n())
}

foo(mtcars$gear)

which results in:

# A tibble: 3 x 2
  Levels     n
   <dbl> <int>
1     3.    15
2     4.    12
3     5.     5
davsjob
  • 1,882
  • 15
  • 10