【问题标题】:Double brackets (i.e., {{}}, curly-curly) do not work within tidyr::complete() and tidyr::nesting()双括号(即 {{}}、卷曲)在 tidyr::complete() 和 tidyr::nesting() 中不起作用
【发布时间】:2021-06-01 13:57:22
【问题描述】:

在我自己的函数中使用时,我无法让双括号(即{{}}、卷曲)在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

有什么想法吗?

【问题讨论】:

    标签: r tidyr


    【解决方案1】:

    你可以把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)))
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 2021-07-26
      • 2017-09-14
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 2020-02-25
      相关资源
      最近更新 更多