When calling the dplyr::n()
function in the global environment, an error occurs.
n()
# Error: This function should not be called directly
This error makes sense and I was curious to see how it was implemented.
n
# function ()
# {
# abort("This function should not be called directly")
# }
# <bytecode: 0x000000001650f200>
# <environment: namespace:dplyr>
To my surprise, however, there is no if
or condition check. Just throw the bug. The same does not occur when we call n()
in your natural habitat .
mtcars %>%
group_by(cyl) %>%
summarise(n = n())
# # A tibble: 3 x 2
# cyl n
# <dbl> <int>
# 1 4 11
# 2 6 7
# 3 8 14
So the questions that remain are two:
n()
function know that it is being called in another context? and n()
count? (where is the source code for that part)