Skip to content

loading .rda file

6 messages · Robert Baer, Martin Maechler, Jeff Newmiller +1 more

#
Hi,

I'm slowly migrating from SAS to R and - for the very first time - I'm
working with a native .Rda data file (rather than importing data from other
sources). When I load this .Rda file into the global environment using
load("file path") I see a data.frame in the global environment called
"mydata" that corresponds to the .rda file.

My question: how can I change the name of this data.frame to something of
my choosing?

Thanks for considering this very simple question.

Leslie
#
I think that the .rda extension is the old extension convention for what 
now gets the .RData extension name by convention.

These are basically workspaces. These .RData files can contain multiple 
data objects, and all objects seem to read back in with the same name 
that they were saved with using the save() function.  Of course, you can 
assign a new name to the objects you read in with the standard <- or -> 
syntax.

See ?save, to lean how to save them with the new name.  You can save 
just an individual data object in an .rda or .RData file and make the 
data object name match the filename if you so wish.
On 8/30/2016 9:37 AM, Leslie Rutkowski wrote:

  
    
#
You cannot. However, you can load the file into a dedicated environment to keep those names separated from your global environment. e.g. [1]

The saveRDS/loadRDS functions are an alternative handle one object at a time without dragging the object names into the picture (you have to name the re-loaded object).

However, the best approach is to write scripts that pull directly from your source (non-R) data files. This makes your work process reproducible as you develop it.

[1] https://stat.ethz.ch/pipermail/r-help/2016-August/441078.html
#
> You cannot. However, you can load the file into a dedicated environment to keep those names separated from your global environment. e.g. [1]

yes, that's my "famous"  only-allowed use of  attach() :
attach() an rda-file to your search patch instead of polluting
your globalenv.

  > The saveRDS / loadRDS 

        saveRDS / readRDS   are the correct names

    > functions are an alternative handle one object at a time without dragging the object names into the picture (you have to name the re-loaded object).

The fact that it is readRDS() and not loadRDS(),
actually does convey via it is often "much better" in the sense
of functional / transparent programming to use this pair in
favor of save() / load() :
readRDS() does *return* what we are interested in, and

   result <- readRDS(<file>)

is so much better than   load(<file>)   silently overwriting all
kinds of objects in my globalenv.

Indeed, I strongly advocate to use  saveRDS() and readRDS()
much more frequently than they are used nowawadays.



    > However, the best approach is to write scripts that pull directly from your source (non-R) data files. This makes your work process reproducible as you develop it.

Yes, but that's sometimes too inefficient.

And then, some of us do simulations and other expensive
computations, we need/want to save and re-read.

and yes,  I *always*  use the equivalent of   q("no")  to leave R; 
never save the workspace or load it at startup, unless
accidentally on non-standard (for me) platforms.

Martin

    > [1] https://stat.ethz.ch/pipermail/r-help/2016-August/441078.html
    > -- 
    > Sent from my phone. Please excuse my brevity.
> On August 30, 2016 7:37:24 AM PDT, Leslie Rutkowski <leslie.rutkowski at gmail.com> wrote:
>> Hi,
    >> 
    >> I'm slowly migrating from SAS to R and - for the very first time - I'm
    >> working with a native .Rda data file (rather than importing data from
    >> other
    >> sources). When I load this .Rda file into the global environment using
    >> load("file path") I see a data.frame in the global environment called
    >> "mydata" that corresponds to the .rda file.
    >> 
    >> My question: how can I change the name of this data.frame to something
    >> of
    >> my choosing?
    >> 
    >> Thanks for considering this very simple question.
    >> 
    >> Leslie
    >> 
    >> [[alternative HTML version deleted]]
    >> 
    >> ______________________________________________
    >> R-help at 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.

    > ______________________________________________
    > R-help at 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.
#
Thanks for the perspective, Martin. I personally feel like attaching to the search path is more confusing than being explicit, because it appears indistinguishable from namespace pollution yet for modifying data it creates copies in the current environment (in-place editing requires `<<-` which requires keeping your search path in mind as you work to know when to use it).
#
Thanks, all, for your quick and comprehensive help with this - very much
appreciated in my R journey.

Leslie

On Tue, Aug 30, 2016 at 8:03 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote: