The cause of "bad magic number" error when loading a workspace and how to avoid it?

RWorkspaceMagic Numbers

R Problem Overview


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.

R Solutions


Solution 1 - R

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

Solution 2 - R

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.

Solution 3 - R

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")

Solution 4 - R

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.

Solution 5 - R

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

Solution 6 - R

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

object <- readRDS("object.rds")

Solution 7 - R

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).

Solution 8 - R

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.

Solution 9 - R

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionN BrouwerView Question on Stackoverflow
Solution 1 - RChris SHView Answer on Stackoverflow
Solution 2 - RjhearnView Answer on Stackoverflow
Solution 3 - Ruser2643170View Answer on Stackoverflow
Solution 4 - REllis ValentinerView Answer on Stackoverflow
Solution 5 - RAurelia AuritaView Answer on Stackoverflow
Solution 6 - RDCZView Answer on Stackoverflow
Solution 7 - RstevecView Answer on Stackoverflow
Solution 8 - RJuan Manuel Ortiz de ZarateView Answer on Stackoverflow
Solution 9 - RmariopeView Answer on Stackoverflow