在R中,如何在使用双卷发括号时选择列?为什么我可以将$运算符与卷发支架使用?

发布于 2025-01-20 15:50:45 字数 1790 浏览 2 评论 0原文

我正在制作一个函数,需要根据用户输入选择特定列。该功能起作用,除了我试图调用用户指定使用$ {{input}}指定的特定列,我收到一条错误消息,即尽管没有,我的函数中有一个额外的'{'。我该如何解决这个问题?为什么我不能在没有触发此错误的情况下用户df $ {{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)

这是一个

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)

示例数据集和 $ {{input}}失败,在这种情况下是什么解决方法。谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め七分饶幸 2025-01-27 15:50:45

基于示例,我们将单列用于time_rangefilter将数据纳入dat_score1dat_score2,基于在time_range和'score_1','score_2'列中的na元素上,在geom_line中使用该元素,用作data 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 -testing

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

-output

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文