An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121107/5beef010/attachment.pl>
save/load and package namespaces
10 messages · Jamie Olson, Jeff Newmiller, David Winsemius +1 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121107/fd5f0d22/attachment.pl>
Stop being surprised. Loaded packages are not part of "envir" (whatever that is), nor are they part of the global environment. You have to reload any packages needed separately from the load call.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Jamie Olson <inspired2apathy at gmail.com> wrote:
Could someone explain to me what namespaces are loaded/saved when objects are saved? Specifically, I'm using this: save(list = ls(all.names = TRUE, envir = envir), file = name, envir = envir) to save out everything from an environment. Later, loading it on another machine, I'm surprised to see the load failing for being unable to load certain packages. Could anyone help me understand why this happens? Jamie Olson [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list 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.
On Nov 7, 2012, at 9:50 AM, Jamie Olson wrote:
Could someone explain to me what namespaces are loaded/saved when objects are saved?
None. That's what require() or library() or source() are for.
Specifically, I'm using this: save(list = ls(all.names = TRUE, envir = envir), file = name, envir = envir) to save out everything from an environment.
You need to read more carefully: ?ls ?objects ?search
Later, loading it on another machine, I'm surprised to see the load failing for being unable to load certain packages. Could anyone help me understand why this happens?
`ls` with default settings only lists data and function objects that the user has defined. The history mechanism could be used to restore packages that were loaded during a session. ?history You should be able to see this by looking at what ls() produces. It does not generally return a listing of items in loaded packages.
David Winsemius, MD Alameda, CA, USA
On 07/11/2012 12:50 PM, Jamie Olson wrote:
Could someone explain to me what namespaces are loaded/saved when objects are saved?
None are loaded or saved when you save the object, but the names of some
are saved. For example,
library(Hmisc) # not normally loaded/attached
x <- zoom # copy a function from Hmisc
save(x, file="x.RData")
This will save a copy of a function from Hmisc to the file, but the
function's environment is the Hmisc namespace. To properly load that
function via
load("x.RData")
R will load the referenced namespace. You will see it appear in
loadedNamespaces() after the load (assuming you still have Hmisc available).
I believe this will also happen if you try to load an S4 object; you'll
need to be able to load the namespace of its class.
Duncan Murdoch
Specifically, I'm using this: save(list = ls(all.names = TRUE, envir = envir), file = name, envir = envir) to save out everything from an environment. Later, loading it on another machine, I'm surprised to see the load failing for being unable to load certain packages. Could anyone help me understand why this happens? Jamie Olson [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121107/adbf7486/attachment.pl>
On 12-11-07 6:20 PM, Jamie Olson wrote:
Thank you! This explains the error thrown by getNamespace() for the missing package. So I imagine this will happen for any function's environment? Do you know if this should happen for S3 objects or just S4?
It should only happen for objects that have an environment associated with them. Functions do, S4 objects do, formulas do, but S3 objects don't (unless they happen to contain something that does). If the environment is globalenv() (the user environment), it's no big deal. It's only when a package namespace is there (as with functions exported from a package) that you create the dependency. Duncan Murdoch
Jamie Olson
On Wed, Nov 7, 2012 at 4:10 PM, Duncan Murdoch <murdoch.duncan at gmail.com
<mailto:murdoch.duncan at gmail.com>> wrote:
On 07/11/2012 12:50 PM, Jamie Olson wrote:
Could someone explain to me what namespaces are loaded/saved
when objects
are saved?
None are loaded or saved when you save the object, but the names of
some are saved. For example,
library(Hmisc) # not normally loaded/attached
x <- zoom # copy a function from Hmisc
save(x, file="x.RData")
This will save a copy of a function from Hmisc to the file, but the
function's environment is the Hmisc namespace. To properly load
that function via
load("x.RData")
R will load the referenced namespace. You will see it appear in
loadedNamespaces() after the load (assuming you still have Hmisc
available).
I believe this will also happen if you try to load an S4 object;
you'll need to be able to load the namespace of its class.
Duncan Murdoch
Specifically, I'm using this:
save(list = ls(all.names = TRUE, envir = envir), file = name,
envir =
envir)
to save out everything from an environment.
Later, loading it on another machine, I'm surprised to see the
load failing
for being unable to load certain packages. Could anyone help me
understand
why this happens?
Jamie Olson
[[alternative HTML version deleted]]
________________________________________________
R-help at r-project.org <mailto:R-help at r-project.org> mailing list
https://stat.ethz.ch/mailman/__listinfo/r-help
<https://stat.ethz.ch/mailman/listinfo/r-help>
PLEASE do read the posting guide
http://www.R-project.org/__posting-guide.html
<http://www.R-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.
5 days later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121113/8f15e963/attachment.pl>
On 13/11/2012 1:45 PM, Jamie Olson wrote:
Correct me if I'm wrong, but it also seems that more generally,
everything works as long as the environment is 'below' .GlobalEnv.
For example,
x = function(){
y = 4
function()y
}
yfun = x()
save(yfun,file = "yfun.RData")
load("yfun.RData")
yfun()
This works fine even when there are more inherited environments. I
imagine this is because in saving yfun, it also saves the environment
and any parent environments until some point? Is it checking to see
if the environment inherits from .GlobalEnv or something like that?
The source is here: https://svn.r-project.org/R/trunk/src/main/serialize.c. It's not the simplest code, but if you look, you can see that the empty, base and global environments are handled specially (by just writing a marker, not their contents). Package and namespace environments are handled by writing out a string describing them. Other environments are saved by saving their parent, their content, their hash table, and their attributes. So yfun would be saved, along with its environment, but the parent of that environment is globalenv(), so just the marker is saved. Duncan Murdoch
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121114/1560516f/attachment.pl>