2

Is there a possibility to define a library that gets loaded, when one reads in a saved object in an .rdata file. E.g.: after running:

library(data.table)
dt1 = data.table(a=1:10,b=letters[1:10])
save(dt1,file="dt.rdata")

dt1 is saved dt.rdata.

Alas when reading that file in to a pristine R session (e.g. by double clicking on the file in windows explorer) dt1 is available, but corresponding data.table commands can only be used after issuing an additional

library(data.table)

in the newly opened R session. Is there some way to define within the data file, that certain packages are to be loaded or some other commands are to be performed before/after reading in the respective file?

jf1
  • 123
  • 5
  • Maybe the easiest is to put that into a function. Something like `myReader <- function(fileName) {; require(data.table); load(fileName);};`. Then read in the data `myReader("dt.rdata")`. – lmo Nov 25 '16 at 17:02
  • Double-clicking on a file does not exactly make it part of a reproducible analysis workflow. It's fine for "one-offs", but in my experience "one-offs" quickly become full on scripts and having all of the proper scaffolding in place (`library()` calls, setup defaults, data read section, data cleaning section, EDA, models, charts) saves a great deal of pain in the long run. – hrbrmstr Nov 25 '16 at 17:11

1 Answers1

1

Save your data into a different workspace and add a function called .First which load the library.

.First <- function(){library(data.frame)}
kaliczp
  • 457
  • 1
  • 12
  • 18