After learning the pattern enquo
-!!
, I got hooked on tidy evaluation: I still don't grok it, but I have to say, it's soooo much better than lazyeval
!! As it always happens when I started learning something new, I found a stumbling block: default parameters. When the symbol which is quoted/unquoted is a function parameter with a default value, I'm not able to get tidy evaluation to work. I guess it has something to do with the concept of lazy evaluation, but the truth is that I don't get what's going on.
Here's an example: don't worry about the function which gets and wrangle data (but definitely feel free to look at the data if you're a football fan, they're fun!). The only functions you need to care about is traceplots_by_factor
:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
library(ggplot2)
library(rio)
# function to get & wrangle data: you don't have to care about this
get_and_wrangle_nfl_data <- function(){
# get the data
download.file("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018-08-28/nfl_2010-2017.csv",
"nfl_2010-2017.csv")
nfl <- import("nfl_2010-2017.csv")
# wrangle the data
nfl$V1 <- NULL
nfl <- nfl %>%
mutate(time = ymd(game_year, truncated = 2L) + months(8) + (game_week - 1) * weeks(1)) %>%
filter(name == name[1]) %>%
gather(key = variable, value = value, -name, -time, -position)
}
# plotting function with defaults
traceplots_by_factor <- function(dataframe_tall, x_var, y_var, var,
factor_var = NULL, factor_values = NULL){
x_var <- enquo(x_var)
y_var <- enquo(y_var)
var <- enquo(var)
if (!is.null(factor_var)) {
factor_var <- enquo(factor_var)
dataframe_tall <- filter(dataframe_tall, !! factor_var %in% factor_values)
}
p <- ggplot(dataframe_tall, aes(x = !! x_var, y = !! y_var)) +
geom_point(color = "blue") +
facet_wrap(vars(!! var), scales = "free_y") +
guides(col = guide_legend(ncol = 1))
p
}
# get & wrangle data
nfl <- get_and_wrangle_nfl_data()
positions <- unique(nfl$position)
# this plot works
traceplots_by_factor(nfl, time, value, variable)
# this doesn't works
traceplots_by_factor(nfl, time, value, variable, factor_var = position, factor_values = positions[1])
#> Error in traceplots_by_factor(nfl, time, value, variable, factor_var = position, : oggetto "position" non trovato
Created on 2018-08-29 by the reprex package (v0.2.0).
In other words, if I let factor_var
have his default value NULL
, the plotting function works runs, but if I try to assign position
to factor_var
, the plotting function doesn't work anymore. Why?