gpt4 book ai didi

r - 如何在 R 中将函数参数捕获为字符串?

转载 作者:行者123 更新时间:2023-12-04 02:31:49 24 4
gpt4 key购买 nike

我想捕获传递给函数的传入参数。我怎样才能做到这一点?下面的功能不是我想要的。我想要的输出是 "Using mtcars and am"。我的印象是 rlang 可以帮助解决这个问题,但我一直没能找到一个函数来完成这项工作。

fx_capture<- function(fx_data, fx_var) {
name_data <- quote(fx_data)
name_var <- quote(fx_var)
paste("Using", name_data, "and", name_var)
}

fx_capture(mtcars, am)
> "Using fx_data and fx_var"

最佳答案

我们可以使用deparse(substitute

fx_capture<- function(fx_data, fx_var) {

name_data <- deparse(substitute(fx_data))
name_var <- deparse(substitute(fx_var))

paste("Using", name_data, "and", name_var)
}

fx_capture(mtcars, am)

或者用match.call

fx_capture<- function(fx_data, fx_var) {

paste0("Using ", do.call(paste, c(lapply(match.call()[-1],
as.character), sep = ' and ')))


}

fx_capture(mtcars, am)

关于r - 如何在 R 中将函数参数捕获为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63836020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com