I am interested in listing objects in an RDATA file and loading only selected objects, rather than the whole set (in case some may be big or may already exist in the environment
Thanks to @Dirk and @Joshua.
I had an epiphany. The command/package foreach
with SMP or MC seems to produce environments that only inherit, but do not seem to conflict with, the global environment.
lsfile <- function(list_files){
aggregate_ls = foreach(ix = 1:length(list_files)) %dopar% {
attach(list_files[ix])
tmpls <- ls(pos = 2)
return(tmpls)
}
return(aggregate_ls)
}
lsfile("f1.rdat")
lsfile(dir(pattern = "*rdat"))
This is useful to me because I can now parallelize this. This is a bare-bones version, and I will modify it to give more detailed information, but so far it seems to be the only way to avoid conflicts, even without ignore.
So, question #1 can be resolved by either ignoring the warnings (as @Joshua suggested) or by using whatever magic foreach
summons.
For part 2, loading an object, I think @Joshua has the right idea - "get" will do.
The foreach
magic can also work, by using the .noexport
option. However, this has risks: whatever isn't specifically excluded will be inherited/exported from the global environment (I could do ls()
, but there's always the possibility of attached datasets). For safety, this means that get()
must still be used to avoid the risk of a naming conflict. Loading into a subenvironment avoids the naming conflict, but doesn't avoid the loading of unnecessary objects.
@Joshua's answer is far simpler than my foreach detour.