How do I add quotations around an input to a function in R?
I would like to have something such as,
function_name (x) {
paste0("The input you have chosen is", x,)
}
function_name(x = gender)
output: "The variable you have chosen is gender"
I know that I could do it using paste0 if the input was
function_name("gender"), but I don't know how to do this if the input doesn't have quotations.
I have tried using paste0 to paste single quotations around the word, but gives errors such as:
Error in paste0("'", x, "'") : object 'Gender' not found.
I have also tried escaping the quotation marks, but the slashes are being read and giving errors as well.
Thanks in advance for any suggestions.
As pointed by AdroMine, this envolves non-standard evaluation. The link he provided is quite useful.
But to provide an answer, you can do the following:
We are capturing the "word"
genderbefore R tries to evaluate it (i.e., tries to find which value should be associated withgender), and then, we are getting the actual text withas.character()