0

I have a few large datasets say fam1997,fam1998,fam1999. When I load for example

load("fam1997")

it returns a data frame called x. Even when I instead use

fam1997 <- load("fam1997")

then

print(fam1997)

it still returns "x"

I don't know why. Is this a bug in load()? or I missed some points? This is quite annoying when I want to load multiple data sets at the same time.

s_baldur
  • 29,441
  • 4
  • 36
  • 69
tobinz
  • 305
  • 1
  • 7
  • Read [`?load`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/load.html), where it states that it returns *"A character vector of the names of objects created, invisibly."* `load` works primarily in side-effect, loading its variables into an environment (defaults to the `.GlobalEnv`). So instead of looking at `fam1997`, look at `x` (since that's what it appears to be loading). – r2evans Mar 17 '21 at 13:33
  • If you want to load raw data directly into a variable like that, you have two options: (1) save a single variable with `saveRDS` and load with `fam1997 <- readRDS("fam1997.rds")`, limited to single objects only; or (2) load into a temp environment and look at it there: `e <- new.env(parent=emptyenv()); load("fam1997.rda", envir=e); e$x` (which doesn't change the fact that it operates in side-effect only, but it avoids cluttering `.GlobalEnv`). – r2evans Mar 17 '21 at 13:35
  • Bottom line, this is not a bug in `load`, it is operating as it was designed to do: in side-effect. Realize that `.rda` files can contain more than one object; if it contained two frames `x` and `y`, what would you expect `fam1997 <- load("fam1997")` to do? Do you think it is reasonable to perform one way when the `.rda` file has one object, and act differently when it has more than one (or no objects)? Should it silently discard the second and subsequent objects? (Neither of those are reasonable, imho.) – r2evans Mar 17 '21 at 13:42
  • Related: https://stackoverflow.com/q/21370132/3358272 – r2evans Mar 17 '21 at 13:45
  • 1
    Thanks! It makes sense! – tobinz Mar 17 '21 at 14:05

0 Answers0