2

How can I pass a character vector naming existing objects to a function that only accepts unquoted names of existing objects?

devtools::use_data(..., )
?use_data: ...   Unquoted names of existing objects to save.

for example an alternative interface is provided by ?save():

base::save(..., list=, ) 
?save: list   A character vector containing the names of objects to be saved.

Example:

x <- "hello"
y <- "world"
save(x, y, file="./test.rda") # works fine
save(c("x", "y"), file="./test.rda")
>> Error in save(c("x", "y"), file = "./test.rda") : Objekt ‘c("x", "y")’ nicht gefunden

I'm sure this has been asked and answered many times but I couldn't find a solution. I unsuccesfully tried with the usual suspects as.name(), noquote(), get(), eval(), parse(), substitute(), etc.

The closest I got was

unquote(c("x", "y")

Thanks for any help or redirection

6
  • 1
    In this specific case you can do : save(list = c("x", "y"), file="./test.rda") Commented Aug 13, 2018 at 9:59
  • 1
    The example you chose might not be the best because save accepts character arguments as well as names. so this would work as well : do.call(function(...) save(...,file="./test.rda"), as.list(c("x", "y"))) though you might want something looking more like do.call(function(...) save(...,file="./test.rda"), lapply(c("x", "y"), as.name)) for other cases. Commented Aug 13, 2018 at 10:05
  • @Moody_Mudskipper Can you sove the problem by not using list= but the ... argument for input, in save(), like I am forced to do in use_data(...,)? Commented Aug 13, 2018 at 12:47
  • That's what I'm doing with the 2nd option of my second comment Commented Aug 13, 2018 at 12:48
  • 1
    Your answer will be enough ;), and better to use the form you used. If you use rlang/tidyverse you can also use invoke(save, lapply(obj, as.name), file="./test.rda") which is less awkward to deal with additional arguments. Commented Aug 13, 2018 at 14:15

1 Answer 1

4

And this would be the answer based on @Moody_Mudskipper's comment

x <- "hello"
y <- "world"
obj <- c("x", "y")
do.call(save, c(lapply(obj, as.name), file="./test.rda"))

analog with devtools::use_data()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.