[R] Fail to save an object using name as string
Jeff Newmiller
jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Tue Apr 10 08:37:47 CEST 2018
Have you ever noticed that when you run
x <- 1:5
y <- 2:6
plot( x, y+1 )
you get the expressions you used in your call to plot on the axis labels? `x` is an expression consisting of a single symbol and y+1 is an expression consisting of the addition operator and two arguments: the symbol x and the constant 1. Plot uses these expressions in two ways: one to get numbers to plot, but also converts the expressions into strings that can be printed on the axes.
The save function has an `...` argument that it expects to find symbol names in, and when you pass an expression as an unnamed argument instead and it then tries to convert the expression to a string and use that as a name for a variable, you get problems.... so don't do that.
The save function also has the named argument `list` that does not have that symbol-name-extracting behaviour. By explicitly passing a vector of character strings (of length one in your case) in the named `list` argument you avoid that special handling applied by the save function to the `...` argument.
It does seem strange at first, but it goes along with some rather convenient features of the R statistical environment like formulas and intuitive plot labeling, and the ability to use
save( Var_2000, file = paste0( "Var_", year, ".Rdata" ) )
with no quotes.
On April 9, 2018 9:11:49 PM PDT, Marc Girondot via R-help <r-help using r-project.org> wrote:
>Dear list member,
>
>I think that I have detected a strange behavior of the save() command:
>
> > year <- "2000"
> > assign(paste0("Var_", year), list(A=10, B=20))
> > get(paste0("Var_", year))
>$A
>[1] 10
>
>$B
>[1] 20
>
># At this point all is ok, I have created a list of name Var_2000
>
> > save(paste0("Var_", year), file=paste0("Var_", year, ".Rdata"))
>Error in save(paste0("Var_", year), file = paste0("Var_", year,
>".Rdata")) :
> objet ‘paste0("Var_", year)’ introuvable
>
>(introuvable=cannot be found)
>
>but
>
> > save("Var_2000", file=paste0("Var_", year, ".Rdata"))
>
>When I read the help for save: ?save
>
>... the names of the objects to be saved (as symbols or character
>strings).
>
>I checked if paste0("Var_", year) produced a character string:
>
> > str("Var_2000")
> chr "Var_2000"
> > str(paste0("Var_", year))
> chr "Var_2000"
>
>The solution was to include the name in the list argument:
>
> > save(list=paste0("Var_", year), file=paste0("Var_", year, ".Rdata"))
>
>But I don't understand what is the difference between "Var_2000" and
>paste0("Var_", year) as both produced the same output.
>
>Thanks for your help,
>
>Marc
>
>
>R Under development (unstable) (2018-03-23 r74448) -- "Unsuffered
>Consequences"
>Copyright (C) 2018 The R Foundation for Statistical Computing
>Platform: x86_64-apple-darwin15.6.0 (64-bit)
>
>______________________________________________
>R-help using 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.
--
Sent from my phone. Please excuse my brevity.
More information about the R-help
mailing list