【发布时间】:2018-04-14 01:53:15
【问题描述】:
我不明白,为什么glue_data 不使用父父环境中可用的变量?请参阅下面的代码。函数test_call_glue_directly 返回所需的结果。函数test_call_glue 返回错误Error in eval(x, envir = data, enclos = envir) : object 'c_1' not found
当然,我的真实函数不止一个变量。 formula_1 可能指的是 {c_2} 和 {c_3}。或{c_456}、{c_7}、{c_8}。 c_* 的计数可能不同。
有没有办法创建专门用于调用这个glue_data的新环境?或者从父环境中提取变量?或者一些更好的方法来组织我的代码?我不想将参数c_1 传递给函数fill_1,因为在我的真实脚本中可能存在未知名称的变量数量。
fill_1 <- function(letts, n0, formula_x) {
purrr::cross_df(list(row = n0, column = letts)) %>%
glue::glue_data(formula_x) %>%
matrix(ncol = length(letts)) %>%
as.data.frame
}
test_call_glue <- function() {
c_1 <- "C"
formula_1 <- "={c_1}{row}"
fill_1(c("A"), c(1:5), formula_1)
}
test_call_glue_directly <- function() {
# The function without wrapping glue_data in function
c_1 <- "C"
formula_1 <- "={c_1}{row}"
purrr::cross_df(list(row = c(1:5), column = c("A"))) %>%
glue::glue_data(formula_1) %>%
matrix(ncol = 1) %>%
as.data.frame
}
test_call_glue_directly()
test_call_glue()
【问题讨论】:
标签: r