Sometimes data files come in .rdata
objects. These are annoying compared to .rds
files because the objects have predefined names. In my case, I want to rename the object automatically and get rid of the wrongly named version. Simple somewhat contrived example:
#make a new iris with a bad name
badnameiris = iris
#save it to a file
save(badnameiris, file = "iris.rdata")
#rename badname version from global envir
rm(badnameiris)
#read iris from file
irisname = load("iris.rdata")
#this variable is not iris, but the name of the variable it was assigned to
irisname
[1] "badnameiris"
#it's to use the right name with get()
goodnameiris = get(irisname)
#but harder to get rid of the wrong one with rm()
rm(irisname)
The last line does not work as intended because it requires a bare name as input and it gets a character vector. I realize one can actually use the list
argument in rm()
, but suppose one could not.
How does one in general convert from character to unquoted for these purposes?
I tried the rlang functions, but these are for non-standard evaluation as used in tidyverse context. I tried as.name()
, as suggested here. Does not work either. Most questions I could find asking this question relate to tidyverse, but I'm trying to do base R context.
(An alternative solution above is to make a function that utilizes the destruction of the local environment to remove the unwanted copy of the object.)