Examining contents of .rdata file by attaching into a new environment - possible?

后端 未结 4 611
再見小時候
再見小時候 2021-01-31 22:48

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

4条回答
  •  [愿得一人]
    2021-01-31 23:24

    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.

提交回复
热议问题