2
When calling the function dplyr::n()
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 play error. Same does not occur when we call n()
in his habitat natural.
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:
- As the function
n()
knows that it is being called in another context? and - As the function
n()
account? (where is the source code of that party)