Search code examples
rggplot2curly-bracesdollar-sign

In R, how do you select a column while using double curly brackets? Why can't I use the $ operator with curly brackets?


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!


Solution

  • 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