row_number() is part of the class of hybrid evaluation functions in dplyr. When possible, these are evaluated in C++ and in the context of the data frame you are mutating / filtering / etc.
The hybrid implementation of row_number() in particular is defined here:
And I think it gets registered here:
In that second link, you can see all the other hybrid functions!
You can actually check if an expression is going to use hybrid evaluation or not (at least in dev dplyr)
suppressPackageStartupMessages(library(dplyr)) # 0.8.0.9000
d <- tibble(a = 1:5)
# A cpp call
hybrid_call(d, row_number())
#> <hybrid evaluation>
#> call : dplyr::row_number()
#> C++ class : dplyr::hybrid::internal::RowNumber0<dplyr::NaturalDataFrame>
# A R call
hybrid_call(d, row_number() + 1)
#> <standard evaluation>
#> call : row_number() + 1
Created on 2019-01-04 by the reprex package (v0.2.1.9000)
RowNumber0 is defined in that first link to the cpp file where all of the row_number() implementation is.
As @pete mentioned, in the newest dplyr there is also some extra code in row_number() using from_context("..group_size") when x is missing. If you try and call that outright, you will be disappointed:
dplyr:::from_context("..group_size")
# Error: NULL should only be called in a data context
But (and you should not do this) use it inside of a mutate() call where the "context" is correct, and you get real results:
suppressPackageStartupMessages(library(dplyr)) # 0.8.0.9000
d <- tibble(a = 1:5)
# using it in the right context
mutate(d, x = dplyr:::from_context("..group_size"))
#> # A tibble: 5 x 2
#> a x
#> <int> <int>
#> 1 1 5
#> 2 2 5
#> 3 3 5
#> 4 4 5
#> 5 5 5
# it just returns the group size
mtcars %>%
group_by(cyl) %>%
mutate(
group_size = dplyr:::from_context("..group_size")
) %>%
select(cyl, group_size)
#> # A tibble: 32 x 2
#> # Groups: cyl [3]
#> cyl group_size
#> <dbl> <int>
#> 1 6 7
#> 2 6 7
#> 3 4 11
#> 4 6 7
#> 5 8 14
#> 6 6 7
#> 7 8 14
#> 8 4 11
#> 9 4 11
#> 10 6 7
#> # … with 22 more rows
Created on 2019-01-04 by the reprex package (v0.2.1.9000)
The moral of the story is, just let dplyr use these hybrid evaluation functions, and there currently is no way for you to access enough information at the R level to create custom ones for your own use.