Use multiple dataframes from a package of data in R

Viewed 165

I am working with a large dataset in an R package.

I need to get all of the separate data frames into my global environment, preferably into a list of data frames so that I can use lapply to do some repetitive operations later.

So far I've done the following:

l.my.package <- data(package="my.package")
lc.my.package <- l.my.package[[3]]
lc.df.my.package <- as.data.frame(lc.my.package)

This effectively creates a data frame of the location and name of each of the .RData files in my package, so I can load them all.

I have figured out how to load them all using a for loop. I create a vector of path names and feed it into the loop:

f <- path('my/path/folder', lc.df.my.package$Item, ext="rdata")
f.v <- as.vector(f)
for (i in f.v) {load(i)}

This loads everything into separate data frames (as I want), but it obviously doesn't put the data frames into a list. I thought lapply would work here, but when I use lapply, the resulting list is a list of character strings (the title of each dataframe with no data included). That code looks like this:

f.l <- as.list(f)
func <- function(i) {load(i)}
df.list <- lapply(f.l, func)

I am looking for one of two possible solutions:

  1. how can I efficiently collect the output of for loop into a list (a "while" loop would likely be too slow)?
  2. how can I adjust lapply so the output includes each entire dataframe instead of just the title of each dataframe?

Edit: I have also tried introducing the "envir=.GlobalEnv" argument into load() within lapply. When I do that, the data frames load, but still not in a list. The list still contains only the names as character strings.

2 Answers

If you are willing to use a packaged solution, I wrote a package call libr that does exactly what you are asking for. Here is an example:


library(libr)

# Create temp directory
tmp <- tempdir()

# Save some data to temp directory
# for illustration purposes
saveRDS(trees, file.path(tmp, "trees.rds"))
saveRDS(rock, file.path(tmp, "rocks.rds"))

# Create library
libname(dat, tmp)
# library 'dat': 2 items
# - attributes: not loaded
# - path: C:\Users\User\AppData\Local\Temp\RtmpCSJ6Gc
# - items:
#    Name Extension Rows Cols   Size        LastModified
# 1 rocks       rds   48    4 3.1 Kb 2020-11-05 23:25:34
# 2 trees       rds   31    3 2.4 Kb 2020-11-05 23:25:34

# Load library
lib_load(dat)

# Examine workspace
ls()
# [1] "dat" "dat.rocks" "dat.trees" "tmp"

# Unload the library from memory
lib_unload(dat)

# Examine workspace again
ls()
# [1] "dat" "tmp"



@rawr's response works perfectly:

df.list <- mget(l.my.package$results[, 'Item'], inherits = TRUE)
Related