124

I tried to load my R workspace and received this error:

Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file ‘WORKSPACE_Wedding_Weekend_September’ has magic number '#gets'
   Use of save versions prior to 2 is deprecated 

I'm not particularly interested in the technical details, but mostly in how I caused it and how I can prevent it in the future. Here's some notes on the situation:

  1. I'm running R 2.15.1 on a MacBook Pro running Windows XP on a bootcamp partition.
  2. There is something obviously wrong this workspace file, since it weighs in at only ~80kb while all my others are usually >10,000
  3. Over the weekend I was running an external modeling program in R and storing its output to different objects. I ran several iterations of the model over the course of several days, eg output_Saturday <- call_model()
  4. There is nothing special to the model output, its just a list with slots for betas, VC-matrices, model specification, etc.
zx8754
  • 52,746
  • 12
  • 114
  • 209
N Brouwer
  • 4,778
  • 7
  • 30
  • 35

10 Answers10

148

I got that error when I accidentally used load() instead of source() or readRDS().

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Chris SH
  • 1,719
  • 1
  • 11
  • 8
  • 4
    So did I, when I accidentally used `load()` instead of `read.csv()`. :p – Waldir Leoncio Feb 12 '14 at 19:31
  • 44
    So did I, part 2, when I accidentally used `load()` instead of `readRDS()` (yes, 9 months later, I'm back here for pretty much the same mistake). – Waldir Leoncio Nov 13 '14 at 17:35
  • 1
    This also happened to me, but the reason was different: a teammate sent me an rds file with an incorrect .rdata extension, so I tried using load(), got the error, then tried readRDS() and it worked. – Bastián Olea Herrera Feb 15 '22 at 18:59
  • Great, I get this error when I use `load` and when I use `readRDS` I get `unknown input format`. Should I just cry? – understorey Nov 24 '22 at 13:10
  • 1
    @BastiánOleaHerrera Thanks, this worked! I had the same problem - someone else had saved files (from about 2 years ago), and I couldn't open them. Simply using `readRDS()` worked - no need to change the file extension from Rdata first. – Earlien Jul 18 '23 at 05:59
47

Also worth noting the following from a document by the R Core Team summarizing changes in versions of R after v3.5.0 (here):

R has new serialization format (version 3) which supports custom serialization of ALTREP framework objects... Serialized data in format 3 cannot be read by versions of R prior to version 3.5.0.

I encountered this issue when I saved a workspace in v3.6.0, and then shared the file with a colleague that was using v3.4.2. I was able to resolve the issue by adding "version=2" to my save function.

jhearn
  • 505
  • 4
  • 6
  • Thanks! This helped in my case (had just installed R from ubuntu repository - and tried to open an RData file I had created a few weeks ago on another machine using a slightly newer version of R) – lebatsnok May 13 '20 at 20:55
20

Assuming your file is named "myfile.ext"

If the file you're trying to load is not an R-script, for which you would use

source("myfile.ext")

you might try the readRDSfunction and assign it to a variable-name:

my.data <- readRDS("myfile.ext")
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
user2643170
  • 201
  • 2
  • 3
10

The magic number comes from UNIX-type systems where the first few bytes of a file held a marker indicating the file type.

This error indicates you are trying to load a non-valid file type into R. For some reason, R no longer recognizes this file as an R workspace file.

Ellis Valentiner
  • 2,136
  • 3
  • 25
  • 36
7

Install the readr package, then use library(readr).

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    Nice trick... I had to try a couple of the packs function but with `readr::` it's easy to scan through the functions. `readr::read_rds` is what worked for me in the end. – Matt Bannert Oct 02 '17 at 20:08
6

It also occurs when you try to load() an rds object instead of using

object <- readRDS("object.rds")
DCZ
  • 1,584
  • 1
  • 12
  • 18
5

I got the error when saved with saveRDS() rather than save(). E.g. save(iris, file="data/iris.RData")

This fixed the issue for me. I found this info here

Also note that with save() / load() the object is loaded in with the same name it is initially saved with (i.e you can't rename it until it's already loaded into the R environment under the name it had when you initially saved it).

luchonacho
  • 6,759
  • 4
  • 35
  • 52
stevec
  • 41,291
  • 27
  • 223
  • 311
2

I had this problem when I saved the Rdata file in an older version of R and then I tried to open in a new one. I solved by updating my R version to the newest.

1

If you are working with devtools try to save the files with:

devtools::use_data(x, internal = TRUE)

Then, delete all files saved previously.

From doc:

internal If FALSE, saves each object in individual .rda files in the data directory. These are available whenever the package is loaded. If TRUE, stores all objects in a single R/sysdata.rda file. These objects are only available within the package.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
mariope
  • 91
  • 1
  • 2
  • 6
0

This error occured when I updated my R and R Studio versions and loaded files I created under my prior version. So I reinstalled my prior R version and everything worked as it should.

Martina
  • 139
  • 1
  • 10