如何在R中将字符作为函数的参数传递?

时间:2019-12-04 07:08:26

标签: r

通常,我们可以在R中编写以下函数:

testFunc <- function(a) {a} # This returns the value of a

如此

testFunc(1)

1

我们如何以这种方式获取testFunc:

argName <- 'a'

testFunc <- function(argName) {a} # This does not work, since argName is required in this function.  

1 个答案:

答案 0 :(得分:1)

#create function without arguments
testFunc <- function() {a}

#add argument
argName <- 'a'
args <- alist(x = )
names(args) <- argName
formals(testFunc) <- args

testFunc
#function (a) 
#{
#  a
#}