Utilities to access the R environment used by saveObject for any given object.

getSaveEnvironment()

recordSaveEnvironment(record)

formatSaveEnvironment()

Arguments

record

Boolean indicating whether to record the environment used to save an object.

Value

formatSaveEnvironment returns a named list containing the current R environment, derived from the sessionInfo. This records the R version, the platform in which R is running, and the versions of all packages as a named list.

When called inside a readObject context (i.e., inside a registered function that is called by readObject) getSaveEnvironment returns a named list of the same structure as that returned by formatSaveEnvironment. This represents the state of the environment used to save the object currently being read by readObject. Alternatively, NULL is returned if this function is called outside of the readObject context, and/or no environment information was recorded for the current object.

If record is not supplied, recordSaveEnvironment returns a boolean indicating whether saveObject should record the environment's details. If record is supplied, it is used to set the environment recording policy, and the previous setting of this value is invisibly returned.

Details

When saving an object, saveObject will automatically record some details about the current R environment. This facilitates trouble-shooting and provides some opportunities for corrective measures if any bugs are found in older saveObject methods. Information about the save environment is stored in an _environment.json file inside the directory containing the object. Subdirectories for child objects may also have separate _environment.json files (e.g., if they were created in a different environment), otherwise it is assumed that they inherit the save environment from the parent object.

Application or extension developers may call getSaveEnvironment from inside a registered function used by readObject or altReadObject. This wil return details of the environment that was used to save the object that is currently being read by the registered function. By accessing the historical environment, developers can check if buggy versions of the corresponding saveObject or altSaveObject methods were used. Appropriate corrective measures can then be applied to recover the correct object, warn users, etc.

Author

Aaron Lun

Examples

# Examine the state of the current save environment:
info <- formatSaveEnvironment()
str(info)
#> List of 4
#>  $ type    : chr "R"
#>  $ version : chr "4.6.1"
#>  $ platform: chr "x86_64-pc-linux-gnu"
#>  $ packages:List of 52
#>   ..$ S4Vectors        : chr "0.51.6"
#>   ..$ BiocGenerics     : chr "0.59.10"
#>   ..$ generics         : chr "0.1.4"
#>   ..$ alabaster.base   : chr "1.13.2"
#>   ..$ jsonlite         : chr "2.0.0"
#>   ..$ compiler         : chr "4.6.1"
#>   ..$ BiocManager      : chr "1.30.27"
#>   ..$ Rcpp             : chr "1.1.2"
#>   ..$ xml2             : chr "1.6.0"
#>   ..$ rhdf5filters     : chr "1.25.3"
#>   ..$ textshaping      : chr "1.0.5"
#>   ..$ systemfonts      : chr "1.3.2"
#>   ..$ yaml             : chr "2.3.12"
#>   ..$ fastmap          : chr "1.2.0"
#>   ..$ jsonvalidate     : chr "1.5.0"
#>   ..$ R6               : chr "2.6.1"
#>   ..$ curl             : chr "7.1.0"
#>   ..$ httr2            : chr "1.3.0"
#>   ..$ knitr            : chr "1.51"
#>   ..$ htmlwidgets      : chr "1.6.4"
#>   ..$ tibble           : chr "3.3.1"
#>   ..$ desc             : chr "1.4.3"
#>   ..$ pillar           : chr "1.11.1"
#>   ..$ rlang            : chr "1.3.0"
#>   ..$ V8               : chr "8.2.0"
#>   ..$ cachem           : chr "1.1.0"
#>   ..$ xfun             : chr "0.60"
#>   ..$ fs               : chr "2.1.0"
#>   ..$ otel             : chr "0.2.0"
#>   ..$ memoise          : chr "2.0.1"
#>   ..$ cli              : chr "3.6.6"
#>   ..$ pkgdown          : chr "2.2.1"
#>   ..$ withr            : chr "3.0.3"
#>   ..$ magrittr         : chr "2.0.5"
#>   ..$ Rhdf5lib         : chr "2.1.0"
#>   ..$ digest           : chr "0.6.39"
#>   ..$ rstudioapi       : chr "0.19.0"
#>   ..$ alabaster.schemas: chr "1.13.0"
#>   ..$ rhdf5            : chr "2.57.3"
#>   ..$ lifecycle        : chr "1.0.5"
#>   ..$ vctrs            : chr "0.7.3"
#>   ..$ downlit          : chr "0.4.5"
#>   ..$ evaluate         : chr "1.0.5"
#>   ..$ glue             : chr "1.8.1"
#>   ..$ whisker          : chr "0.4.1"
#>   ..$ ragg             : chr "1.5.2"
#>   ..$ fansi            : chr "1.0.7"
#>   ..$ rmarkdown        : chr "2.31"
#>   ..$ purrr            : chr "1.2.2"
#>   ..$ tools            : chr "4.6.1"
#>   ..$ pkgconfig        : chr "2.0.3"
#>   ..$ htmltools        : chr "0.5.9"

# Let's mock up an output directory containing an environment file,
# as if it were created by a top-level saveObject() call:
tmp <- tempfile()
dir.create(tmp)
saveObjectFile(tmp, "foobar")
write(
    file=file.path(tmp, "_environment.json"),
    jsonlite::toJSON(info, pretty=4, auto_unbox=TRUE)
)

# Within readObject functions, we can call getSaveEnvironment()
# to get the state of the environment that was used to save the object.
registerReadObjectFunction("foobar", function(x, metadata, ...) {
    cat("Hi I'm loading a FOOBAR object, saved with the following environment:\n")
    print(getSaveEnvironment())
    "FOOBAR"
})

readObject(tmp)
#> Hi I'm loading a FOOBAR object, saved with the following environment:
#> $type
#> [1] "R"
#> 
#> $version
#> [1] "4.6.1"
#> 
#> $platform
#> [1] "x86_64-pc-linux-gnu"
#> 
#> $packages
#> $packages$S4Vectors
#> [1] "0.51.6"
#> 
#> $packages$BiocGenerics
#> [1] "0.59.10"
#> 
#> $packages$generics
#> [1] "0.1.4"
#> 
#> $packages$alabaster.base
#> [1] "1.13.2"
#> 
#> $packages$jsonlite
#> [1] "2.0.0"
#> 
#> $packages$compiler
#> [1] "4.6.1"
#> 
#> $packages$BiocManager
#> [1] "1.30.27"
#> 
#> $packages$Rcpp
#> [1] "1.1.2"
#> 
#> $packages$xml2
#> [1] "1.6.0"
#> 
#> $packages$rhdf5filters
#> [1] "1.25.3"
#> 
#> $packages$textshaping
#> [1] "1.0.5"
#> 
#> $packages$systemfonts
#> [1] "1.3.2"
#> 
#> $packages$yaml
#> [1] "2.3.12"
#> 
#> $packages$fastmap
#> [1] "1.2.0"
#> 
#> $packages$jsonvalidate
#> [1] "1.5.0"
#> 
#> $packages$R6
#> [1] "2.6.1"
#> 
#> $packages$curl
#> [1] "7.1.0"
#> 
#> $packages$httr2
#> [1] "1.3.0"
#> 
#> $packages$knitr
#> [1] "1.51"
#> 
#> $packages$htmlwidgets
#> [1] "1.6.4"
#> 
#> $packages$tibble
#> [1] "3.3.1"
#> 
#> $packages$desc
#> [1] "1.4.3"
#> 
#> $packages$pillar
#> [1] "1.11.1"
#> 
#> $packages$rlang
#> [1] "1.3.0"
#> 
#> $packages$V8
#> [1] "8.2.0"
#> 
#> $packages$cachem
#> [1] "1.1.0"
#> 
#> $packages$xfun
#> [1] "0.60"
#> 
#> $packages$fs
#> [1] "2.1.0"
#> 
#> $packages$otel
#> [1] "0.2.0"
#> 
#> $packages$memoise
#> [1] "2.0.1"
#> 
#> $packages$cli
#> [1] "3.6.6"
#> 
#> $packages$pkgdown
#> [1] "2.2.1"
#> 
#> $packages$withr
#> [1] "3.0.3"
#> 
#> $packages$magrittr
#> [1] "2.0.5"
#> 
#> $packages$Rhdf5lib
#> [1] "2.1.0"
#> 
#> $packages$digest
#> [1] "0.6.39"
#> 
#> $packages$rstudioapi
#> [1] "0.19.0"
#> 
#> $packages$alabaster.schemas
#> [1] "1.13.0"
#> 
#> $packages$rhdf5
#> [1] "2.57.3"
#> 
#> $packages$lifecycle
#> [1] "1.0.5"
#> 
#> $packages$vctrs
#> [1] "0.7.3"
#> 
#> $packages$downlit
#> [1] "0.4.5"
#> 
#> $packages$evaluate
#> [1] "1.0.5"
#> 
#> $packages$glue
#> [1] "1.8.1"
#> 
#> $packages$whisker
#> [1] "0.4.1"
#> 
#> $packages$ragg
#> [1] "1.5.2"
#> 
#> $packages$fansi
#> [1] "1.0.7"
#> 
#> $packages$rmarkdown
#> [1] "2.31"
#> 
#> $packages$purrr
#> [1] "1.2.2"
#> 
#> $packages$tools
#> [1] "4.6.1"
#> 
#> $packages$pkgconfig
#> [1] "2.0.3"
#> 
#> $packages$htmltools
#> [1] "0.5.9"
#> 
#> 
#> [1] "FOOBAR"

registerReadObjectFunction("foobar", NULL)