I have been looking around posts about passing arguments to dplyr functions inside a custom function, and I could not solve the following situation:
I have created the following function to get a subset of a dataframe.
library(Lahman)
top_leaders <- function(df, metric, n) {
# metric is the name of the column of Batting df which I would like to analyze
# n is the number of top players leaders on that metric
stat_leader <- enquo(metric)
df %>%
dplyr::select(playerID, !!stat_leader) %>%
dplyr::top_n(n)
}
As this function works well subsetting the n players leaders on that stat. For example:
> top_leaders(Lahman::Batting, "R", 5)
Selecting by R
playerID R
1 oneilti01 167
2 brownto01 177
3 hamilbi01 198
4 ruthba01 177
5 gehrilo01 167
Nevertheless, I want the result to be ordered, so I use include the arrange
function to order it by the stat.
top_leaders <- function(df, metric, n) {
stat_leader <- enquo(metric)
df %>%
dplyr::select(playerID, !!stat_leader) %>%
dplyr::top_n(n) %>%
dplyr::arrange(desc(!!stat_leader))
}
But it gives the following error:
Selecting by R
Error: incorrect size (1) at position 1, expecting : 5
I tried later to use arrange_(desc(!!stat_leader))
getting also another error:
Selecting by R
Error: Quosures can only be unquoted within a quasiquotation context.
# Bad:
list(!!myquosure)
# Good:
dplyr::mutate(data, !!myquosure)
So I have no ideia on how to solve this.
R
instead of"R"
? That's the convention in thesedplyr
-based functions – camille