Write a file using `saveRDS()` so that it is backwards compatible with old versions of R
Expanding on my comment with a demo:
$ Rscript --version | head -1
R scripting front-end version 3.6.0 (2019-04-26)
$ Rscript -e 'saveRDS(1:10, file="foo.rds")'
$
$ docker run --rm -ti r-base:3.4.0 Rscript --version | head -1
R scripting front-end version 3.4.0 (2017-04-21)
$ docker run --rm -ti -v ${PWD}:/work -w /work r-base:3.4.0 Rscript -e 'print(readRDS("foo.rds"))'
Error in readRDS("foo.rds") :
cannot read workspace version 3 written by R 3.6.0; need R 3.5.0 or newer
Calls: print -> readRDS
Execution halted
$
$ Rscript -e 'saveRDS(1:10, file="foo.rds", version=2)'
$ docker run --rm -ti -v ${PWD}:/work -w /work r-base:3.4.0 Rscript -e 'print(readRDS("foo.rds"))'
[1] 1 2 3 4 5 6 7 8 9 10
$
I use my normal R version which I show to be 3.6.0, and I then launch R 3.4.0 via Rocker, also showing its version.
As expected, it first fails -- and once the data is resaved with version=2
it works as is should.
As I understand from here you have to set version = 2
in saveRDS
:
Serialization format version 3 becomes the default for serialization and saving of the workspace (save(), serialize(), saveRDS(), compiler::cmpfile()). Serialized data in format 3 cannot be read by versions of R prior to version 3.5.0. Serialization format version 2 is still supported and can be selected by version = 2 in the save/serialization functions. The default can be changed back for the whole R session by setting environment variables R_DEFAULT_SAVE_VERSION and R_DEFAULT_SERIALIZE_VERSION to 2. For maximal back-compatibility, files ‘vignette.rds’ and ‘partial.rdb’ generated by R CMD build are in serialization format version 2, and resave by default produces files in serialization format version 2 (unless the original is already in format version 3).