Bowen Chen
Bowen Chen

Reputation: 5

How to pass a character as arguments of a function in R?

Normally, we can write a following function in R:

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

such that

testFunc(1)

1

How can we get the testFunc in this way:

argName <- 'a'

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

Upvotes: 0

Views: 121

Answers (1)

Roland
Roland

Reputation: 132676

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

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

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

Upvotes: 1

Related Questions