[R] Force argument to have quotes

Boris Steipe boris.steipe at utoronto.ca
Tue Jun 6 20:34:40 CEST 2017


If I understand you correctly what you are really asking is how to embed quotes in a string so that it can be parse()'d as an expression. The answer would be: escape the quotes.


R > myOptions <- "Hello"
R > eval(parse(text = paste( "print(", myOptions, ")" )))
 Error in print(Hello) : object 'Hello' not found 

R > eval(parse(text = paste( "print( \"", myOptions, "\")", sep="" )))
[1] "Hello"

(Myself, I would use sprintf(),

R > myFunction <- "print"
R > eval(parse(text = sprintf("%s(\"%s\")", myFunction, myOptions)))
[1] "Hello"

)


Ask again if this is only part of the problem.

B.




> On Jun 6, 2017, at 11:01 AM, Doran, Harold <HDoran at air.org> wrote:
> 
> 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, 
> -5L))
> 
> ### 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
> 	result
> 	}
> 
> 
> ### 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
> Harold
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list