Skip to content

Instantly share code, notes, and snippets.

@dvanic
Last active September 20, 2016 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvanic/77c04a3a3b5cfe6f728f433ec1a92b96 to your computer and use it in GitHub Desktop.
Save dvanic/77c04a3a3b5cfe6f728f433ec1a92b96 to your computer and use it in GitHub Desktop.
Character to variable and variable to character conversion #R
# Variable name to character
deparse(substitute(variable))
"variable"
# Character to variable
eval(as.name("variable"))
str(eval(as.name("variable"))) # should work
# More complex case - when we're sticking it all into a function
outerfunction <- function(resultsdataframe, prefix){
bp <- innerfunction(resultsdataframe, prefix)
return(bp)
}
innerfunction <- function(resultsdataframe, prefix){
# if the resultsdataframe = "EBvsS" i.e. class(resultsdataframe) == "character"
# the value (i.e. actual data frame)
print(eval(as.name(eval(resultsdataframe))))
# its name
print(resultsdataframe)
# below if it points directly to data frame - the actual data
#print(eval(as.name(substitute(resultsdataframe))))
# can't seem to get it to play nice and return the name
}
doIget <- outerfunction( resultsdataframe = "lala", prefix = "whatever")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment