[R] Force argument to have quotes

Doran, Harold HDoran at air.org
Tue Jun 6 17:01:11 CEST 2017

I am writing a program where non-technical R users will read in a config file and the config file will then parse the arguments found within the config and pass them to respective functions. I'm having trouble (efficiently) writing a piece of code to retain quotation marks around the argument which requires it as input, as found in the example function below, myFuncton1.

Below is a minimal, reproducible example of the issue with comments.

### This is a sample structure of the configuration file

scoreConfig <- structure(list(Function = c("myFunction1", "myFunction1", "myFunction1", 
"myFunction2", "myFunction2"), Argument = c("arg1", "arg2", "arg3", 
"arg1", "arg2"), Value = c("5", "10", "Hello", "5", "10"), Class = c("numeric", 
"numeric", "character", "numeric", "numeric")), .Names = c("Function", 
"Argument", "Value", "Class"), class = "data.frame", row.names = c(NA, 

### Two sample functions, once of which requires a string
myFunction1 <- function(arg1, arg2, arg3 = c('Hello', 'Goodbye')){
	arg3 <- match.arg(arg3)
	result <- arg1 + arg2
	cat(arg3, result, '\n')
myFunction2 <- function(arg1, arg2){
	result <- arg1 * arg2

### Working Example. 
### myFunction2 works no problem
myFunction2Vals <- subset(scoreConfig, Function == 'myFunction2')
myOptions <- with(myFunction2Vals, paste(Argument, Value, sep = '=', collapse = ','))
eval(parse(text = paste( "myFunction2(", myOptions, ")" )))

### myFunction1 fails 
myFunction1Vals <- subset(scoreConfig, Function == 'myFunction1')
myOptions <- with(myFunction1Vals, paste(Argument, Value, sep = '=', collapse = ','))
eval(parse(text = paste( "myFunction1(", myOptions, ")" )))

### What I want is simply
myFunction1(arg1 = 1, arg2 = 2, arg3 = 'Hello')

I'm curious if someone has a perspective on the most efficient way to automate this by using information provided in the 'Value" column, so perhaps conditional on that value it could wrap the name in quotes.

I admit to running into a limit and am tapping out so to speak on the right way to do this.

Thanks for any advice

More information about the R-help mailing list