双括号(即 {{}},大括号)在 tidyr::complete() 和 tidyr::nesting() 中不起作用
Double brackets (i.e., {{}}, curly-curly) do not work within tidyr::complete() and tidyr::nesting()
在我自己的函数中使用时,我无法在 tidyr::complete()
和 tidyr::nesting()
中使用双括号(即 {{}}
,大括号)。此代码有效:
library(tidyverse)
cw_subset <- ChickWeight[, c("Chick", "Time", "weight")]
cw_complete <- cw_subset %>%
complete(Time = seq(min(Time), max(Time), by = 1),
nesting(Chick))
但是,如果我尝试创建一个函数来做同样的事情:
complete_data <- function(x, variable){
x %>%
complete(Time = seq(min(Time), max(Time), by = 1),
nesting({{variable}}))
}
cw_complete <- cw_subset %>%
complete_data(variable = Chick)
我收到以下错误:
Error in eval_tidy(dots[[i]], data = out) : object 'variable' not found
有什么想法吗?
你可以把complete
写成-
library(dplyr)
library(tidyr)
complete_data <- function(x, variable){
x %>% complete(Time = seq(min(Time), max(Time), by = 1), {{variable}})
}
cw_complete <- cw_subset %>% complete_data(variable = Chick)
要使用 nesting
,您可以使用 ensym
-
complete_data <- function(x, variable){
x %>%
complete(Time = seq(min(Time), max(Time), by = 1),
nesting(!!ensym(variable)))
}
在我自己的函数中使用时,我无法在 tidyr::complete()
和 tidyr::nesting()
中使用双括号(即 {{}}
,大括号)。此代码有效:
library(tidyverse)
cw_subset <- ChickWeight[, c("Chick", "Time", "weight")]
cw_complete <- cw_subset %>%
complete(Time = seq(min(Time), max(Time), by = 1),
nesting(Chick))
但是,如果我尝试创建一个函数来做同样的事情:
complete_data <- function(x, variable){
x %>%
complete(Time = seq(min(Time), max(Time), by = 1),
nesting({{variable}}))
}
cw_complete <- cw_subset %>%
complete_data(variable = Chick)
我收到以下错误:
Error in eval_tidy(dots[[i]], data = out) : object 'variable' not found
有什么想法吗?
你可以把complete
写成-
library(dplyr)
library(tidyr)
complete_data <- function(x, variable){
x %>% complete(Time = seq(min(Time), max(Time), by = 1), {{variable}})
}
cw_complete <- cw_subset %>% complete_data(variable = Chick)
要使用 nesting
,您可以使用 ensym
-
complete_data <- function(x, variable){
x %>%
complete(Time = seq(min(Time), max(Time), by = 1),
nesting(!!ensym(variable)))
}