【发布时间】:2020-02-24 00:50:31
【问题描述】:
当我尝试将参数传递给使用 enquo() 的函数时,出现以下错误。
“错误:只能在 quasiquotation 上下文中不引用引文。”
在下面的例子中,
Wrap1 函数使用 map 为单个函数运行多个参数集。使用 !!enquo() 传递变量有效。
Wrap 2 尝试传递多个函数和参数,但我无法获得正确的语法。
-
我的传递方式是否存在问题(exec 或 invoke_map 函数中的顺序,或者列如何通过 !!enquo(cols) 传递?
-
有没有办法将额外的变量传递给 invoke_map 或参数列表之外的所有变量(类似于在下面的 wrap1 代码中使用 partial)?
library(dplyr)
library(lubridate)
library(purrr)
library(rlang)
library(rkt)
dataset <- tibble(Date= lubridate::decimal_date(seq(as.Date("2000/1/1"), by = "month", length.out = 120)),
Value = rnorm(120),
season = lubridate::month(seq(as.Date("2000/1/1"), by = "month", length.out = 120)))
#inner function
run1_fun <- function(df, yCOL, datesCOL, seasCOL=NULL, ind.obs=TRUE){
y <- df %>% pull(!!enquo(yCOL))
dates <- df %>% pull(!!enquo(datesCOL))
mk <- rlang::quo_is_null(enquo(seasCOL))
if (mk){
seas <- rep(1, length(y)) #just so can get nseas=1 later
smk <- rkt::rkt(date=dates, y=y)
} else {
seas <- df %>% pull(!!enquo(seasCOL))
smk <- rkt::rkt(date=dates, y=y, block=seas, correct = !ind.obs)
}
out <- tibble(tau = smk$tau,
slope = smk$B,
type = ifelse(mk, "MK", "SK"))
out
}
#inner function with by option
run1_fun_by <- function(df, by=NULL, yCOL, datesCOL, seasCOL=NULL, ind.obs=TRUE){
if(is.null(by)) {
df <- run1_fun(df, !!enquo(yCOL), !!enquo(datesCOL), !!enquo(seasCOL),ind.obs)
} else {
df <- plyr::ddply(df, .variables = by,
.fun = run1_fun, !!enquo(yCOL), !!enquo(datesCOL), !!enquo(seasCOL),ind.obs)
}
df
}
#to run:
run1_fun_by(dataset, by="season", Value, Date, ind.obs=TRUE)
#WRAP 1: WORKS
# example of wrap function that passing multiple arguments sets to the above inner function that uses enquo variables.
wrap1 <- function(df, yCOL, datesCOL, seasCOL=NULL, ind.obs=TRUE,
ttype=c("MK", "SK")){
fun_args <- partial(run1_fun, df=df, yCOL = !!enquo(yCOL), datesCOL = !!enquo(datesCOL), ind.obs=ind.obs)
#fun_args - order in "SK", "MK"
keep <- which(c("SK", "MK") %in% ttype)
args <- list(seasCOL = dplyr::vars(!!enquo(seasCOL), NULL)[keep]) # didnt work with alist..
#args <-tibble(seasCOL = dplyr::vars(!!enquo(seasCOL), NULL)[keep]) #works too
res <- purrr::pmap_dfr(args, fun_args)
res
}
#to run:
wrap1(dataset, Value, Date, season, ind.obs=TRUE, ttype=c("MK", "SK"))
#WRAP 2: DOESN'T WORK
#attempt to iterate through multiple functions and argument sets (where functions requires enquo arguments)
using either invoke_map or exec?
wrap2 <- function(df, yCOL, datesCOL, seasCOL=NULL, ind.obs=TRUE,
ttype=c("MK", "SK", "MKSeas")){
t <- c("SK", "MK", "MKSeas")
#HOW CAN I PASS (which are enq in next function)
sim <- tribble(
~f, ~params,
"run1_fun", list(seasCOL = dplyr::vars(!!enquo(seasCOL)), ind.obs=ind.obs), #SK
"run1_fun", list(seasCOL = dplyr::vars(NULL)), #MK
"run1_fun_by", list(by="season")) #MKSeas
#with invoke_map - but looks like this is retired
res <- invoke_map_dfc(sim$f, sim$params, df=df, yCOL=!!enquo(yCOL), datesCOL=!!enquo(datesCOL))
#with exec - new
#res <- map2_dfc(sim$f, sim$params, function(fn, args) exec(fn, !!!args))
res
}
#to run
wrap2(dataset, Value, Date, season, ind.obs=TRUE, ttype=c("MK", "SK", "MKSeas"))
错误:Quosures 只能在 quasiquotation 上下文中不被引用。
不好:
列表(!!myquosure)
好:
dplyr::mutate(数据, !!myquosure)
致电rlang::last_error() 查看回溯
调用自:abort(paste_line("Quosures 只能在 quasiquotation 上下文中不被引用。",
"", "# Bad:", "list(!!myquosure)",
"", " # Good:", " dplyr::mutate(data, !!myquosure)")
【问题讨论】: