Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a file using `saveRDS()` so that it is backwards compatible with old versions of R

Tags:

r

My compute cluster recently updated to R version R 3.6.0 and deleted the old versions of R. I had been running my project in R 3.4.0. I decided this would be fine, ran some code, and saved the output during my R 3.6.0 session as:

saveRDS(output, output.path)

This file was then transferred to a different computer where interactive R use takes place. This computer runs R/3.4.0, and updating the version of R is not an option. When I go to open the above file, I get the following error:

readRDS(output.path)
Error in readRDS(output.path) : cannot read workspace version 3 written by R 3.6.0; need R 3.5.0 or newer

This is a bummer. I am not the system administrator on either of these computers, so I can't just synchronize the versions. This is my question:

Is there a way to write a file using saveRDS() in R 3.6.0 such that it would be backward compatible in an R 3.4.0 environment?

like image 993
colin Avatar asked Jun 21 '19 13:06

colin


1 Answers

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.

like image 71
Dirk Eddelbuettel Avatar answered Sep 23 '22 15:09

Dirk Eddelbuettel