在R中,如何在使用双卷发括号时选择列?为什么我可以将$运算符与卷发支架使用?
我正在制作一个函数,需要根据用户输入选择特定列。该功能起作用,除了我试图调用用户指定使用$ {{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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于示例,我们将单列用于
time_range
,filter
将数据纳入dat_score1
和dat_score2
,基于在time_range
和'score_1','score_2'列中的na元素上,在geom_line
中使用该元素,用作data
data-testing -testing
-output
Based on the example, we use a single column for
time_range
,filter
the data intodat_score1
anddat_score2
, based on thetime_range
and the NA elements in 'score_1', 'score_2' columns, use that ingeom_line
asdata
-testing
-output