2

I'm making a function where I need to select a specific column based on user input. The function works except that I'm trying to call the specific column a user has specified with ${{input}}, I get an error message that there is an extra '{' in my function, despite there not being. How do I workaround this? And why can't I user df${{input}} without triggering this error?

Here's a sample dataset and the function that works before I user the ${{input}}:

#Sample data and packages

library(dplyr)
library(lubridate)
library(ggplot2)

test <- tibble(Month = ymd(c('1990-01-01', '1990-02-01', '1990-03-01', '1990-04-01', '1990-05-01', '1990-06-01')),
               score_1 = c(1:6),
               score_2 = c(60, 50, 40, 30, NA, 10))

#Working function without using df${{input}} within the geom_line() call

make_chart <- function(data, time_range = c(Week, Month), start_date = NA_Date_) {
  
  data %>%
  ggplot(aes(x = {{time_range}})) + 
    geom_line(data=test[!is.na(test$score_1) & test$Month >= start_date,], aes(y = score_1, colour = "red", linetype = "score 1"), size= 1) + 
    geom_line(data=test[!is.na(test$score_2) & test$Month >= start_date,], aes(y = score_2, colour = "blue", linetype = "score 2"), size= 1)
  
}

make_chart(data = test, start_date = '1990-02-06', time_range = Month)

And here's what I think should work, but doesn't:

library(dplyr)
library(lubridate)
library(ggplot2)

#Note: the change is within the 2 geom_line lines
make_chart <- function(data, time_range = c(Week, Month), start_date = NA_Date_) {
  
  data %>%
  ggplot(aes(x = {{time_range}})) + 
    geom_line(data=test[!is.na(test$score_1) & test${{time_range}} >= start_date,], aes(y = score_1, colour = "red", linetype = "score 1"), size= 1) + 
    geom_line(data=test[!is.na(test$score_2) & test${{time_range}} >= start_date,], aes(y = score_2, colour = "blue", linetype = "score 2"), size= 1)
  
}

make_chart(data = test, start_date = '1990-02-06', time_range = Month)

I ideally would like an answer that explains why df${{input}} fails and what a workaround is for this instance. Thank you!

J.Sabree
  • 2,280
  • 19
  • 48
  • 1
    You are passing data as `test` and within the function, you are again using `test` instead of `data – akrun Apr 11 '22 at 19:56
  • 1
    The `{{}}` is used mainly when you are passing unquoted values.The default argument you had is a vector `c(Week, Month)`, instead it can be just `Month`, also, for subsetting purpose, the entry can be converted to character and then use `.data[[time_range]]` – akrun Apr 11 '22 at 19:59
  • @akrun, I removed it for simplicity, but Week is another column. So, I'm letting users pick which time frame they want they data to be. – J.Sabree Apr 11 '22 at 20:10
  • 1
    The `{{ }}` syntax is not a base R syntax. It is something that is interpreted by the `rlang` package and will only work with functions that use `rlang` to evaluate their expressions . You can't use `$` with variables. See https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-character-value. And you should avoid using `$` with ggplot in general. Use `subset` or `filter` to subset your data. – MrFlick Apr 11 '22 at 20:11

1 Answers1

3

Based on the example, we use a single column for time_range, filter the data into dat_score1 and dat_score2, based on the time_range and the NA elements in 'score_1', 'score_2' columns, use that in geom_line as data

library(lubridate)
library(dplyr)
library(ggplot2)

make_chart <- function(data, time_range = Month, start_date = NA_Date_) {
  
  dat_score1 <- data %>%
           filter(complete.cases(score_1), {{time_range}} >= as.Date(start_date))
  dat_score2 <- data %>%
                    filter(complete.cases(score_2),
      {{time_range}} >= as.Date(start_date))
           
  data %>%
  ggplot(aes(x = {{time_range}})) + 
    geom_line(data= dat_score1, 
        aes(y = score_1, colour = "red", 
          linetype = "score 1"), size= 1) + 
    geom_line(data=dat_score2, 
            aes(y = score_2, colour = "blue", linetype = "score 2"), size= 1)
  
}

-testing

make_chart(data = test, time_range = Month, start_date = '1990-02-06' )

-output

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662