[R] R code in RData

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Thu Oct 28 17:24:46 CEST 2021


>>>>> Bert Gunter 
>>>>>     on Wed, 27 Oct 2021 10:47:14 -0700 writes:

    > See ?load, but you may be confused. Strictly speaking, there is no code in
    > an .Rdata file, only a (typically binary, but possibly ascii)
    > representation of objects, usually as produced by ?save. Of course,
    > functions are also objects, so that if you load a file with functions, the
    > function code is available.

    > You can save and load command histories via ?savehistory, which, like ?save
    > and ?load can usually be accessed through any GUI interface that you are
    > using.

    > Warning: I believe the above is correct, but I may be wrong in at least
    > some details, so give others a chance to reply and possibly correct.

    > Cheers,
    > Bert

    > Bert Gunter

In spite of really not recommending to work with .RData but
rather with R *scripts*  (or R Sweave / R markdown documents),
an often forgotten considerably more flexible and in that sense better
alternative to load() in such situations is  attach()
which creates an environment in which the objects are loaded and
attaches that to the search() path.

Example (from my current R console):

> save.image() # creates .RData
> attach(".RData")
> ls.str(pos=2)
logi :  Named logi [1:3] FALSE NA TRUE
Mlibrary : function (pkg, lib = NULL, check64.32 = TRUE, ...)  
ncF :  int [1:5, 1:3] 5 6 2 1 15 5 6 2 1 15 ...
nchars : function (x, ...)  
ncT :  int [1:5, 1:3] 5 6 NA 1 15 5 6 NA 1 15 ...
x :  chr [1:5] "asfef" "qwerty" NA "b" "stuff.blah.yech"
> 

So, indeed,  ls.str()  comes handy as well..

Best,
Martin



More information about the R-help mailing list