[R] save(), load(), saveRDS(), and readRDS()

Jorgen Harmse JH@rm@e @end|ng |rom roku@com
Fri Sep 29 21:44:47 CEST 2023

Ivan Krylov points out that load(file, e <- new.env()) is cumbersome. I put it into a function.

Jorgen Harmse.

#' Save & load lists & environments


#' \code{\link{save}} has to be told what to save from an environment, and the obvious way

#' to save a structure creates an extra layer. \code{\link{load}} with default settings

#' clobbers the current environment. \code{save.env} saves a list or environment without an

#' extra layer, and by default saves everything. \code{load.env} loads into an environment,

#' and \code{load.list} loads into a list.


#' @param S something that can be coerced to an environment, e.g. a named \code{list}

#' @param file,envir inputs to \code{save} or \code{load}

#' @param list input to \code{save}

#' @param skip variables in \code{envir} that should not be saved, ignored if \code{list}

#' is provided

#' @param ... inputs to \code{load.env} or additional inputs to \code{save}


#' @return \code{invisible} from \code{save.env}; an \code{environment} from \code{load.env};

#' a \code{list} from \code{load.list}


#' @export

save.env <- function( S, file, list = setdiff(ls(envir),skip),

                      envir = if(missing(S)) parent.frame() else as.environment(S),

                      skip=NULL, ...


{ save(list=list, file=file, envir=envir, ...)}

#' @rdname save.env


#' @param keep,remove names of variables to keep or to remove

#' @param absent what to do if variables named in \code{keep} are absent

#' @param parent input to \code{\link{new.env}}


#' @note \code{remove} is forced after the file is loaded, so the default works correctly.


#' @export

load.env <- function( file, keep, remove = if(!missing(keep)) setdiff(ls(envir),keep),

                      absent=c('warn','ignore','stop'), envir=new.env(parent=parent),



{ load(file,envir)


  if ( !missing(keep) && (match.arg(absent) -> absent) != 'ignore'

      && length(keep.absent <- setdiff(keep,ls(envir))) > 0L )

  { print(keep.absent)

    if (absent=='warning')

      warning('The variables listed above are absent from the file.')


      stop('The variables listed above are absent from the file.')




#' @rdname save.env


#' @param all.names input to \code{\link{as.list}}


#' @export

load.list <- function(..., all.names=TRUE) as.list(all.names=all.names, load.env(...))


Message: 2
Date: Fri, 29 Sep 2023 11:42:37 +0300
From: Ivan Krylov <krylov.r00t using gmail.com>
To: Shu Fai Cheung <shufai.cheung using gmail.com>
Cc: R mailing list <r-help using r-project.org>
Subject: Re: [R] save(), load(), saveRDS(), and readRDS()
Message-ID: <20230929114237.2592975a using Tarkus>
Content-Type: text/plain; charset="utf-8"

On Thu, 28 Sep 2023 23:46:45 +0800
Shu Fai Cheung <shufai.cheung using gmail.com> wrote:

> In my personal work, I prefer using saveRDS() and loadRDS() as I
> don't like the risk of overwriting anything in the global
> environment.

There's the load(file, e <- new.env()) idiom, but that's potentially
a lot to type.


	[[alternative HTML version deleted]]

More information about the R-help mailing list