Fixing the R error – bad restore file magic number (file may be corrupted) — no data loaded

This error message occurs in the r language when you have saved r objects to your computer’s hard drive using the saveRDS function, and you then try to read it using the load function. To fix this problem you need to read it using the readRDS function. This is a problem that results from saving in one format and trying to read it in another.

Description of the R error

Unfortunately, looking at this error message, can give you the impression that it results from a problem with a data block on your disk. However, it is not caused by a bad block of data, bad sectors on your hard disk, a problem with the file’s checksum, a problem with your file system, or a problem with your operating system. The r programming Language has several formats for files, and each file type has a different load and save versions. A data file uses the load and save functions, a csv file uses read.csv and write.csv functions, and an rds file uses the readRDS and saveRDS functions. If you try to load a file with the wrong function you will get an error message. You use these file types for storing objects in your program to disk, but they can also be used to create a temporary file or a configuration file if needed. When saving objects all you need is the file name, if you intend to save it in the program’s root directory, however, you can also include a file path to tell your device where to put it.

Explanation of the R error

Here are three code examples that illustrate what causes this error message.

> x = c(1:5,3)
> y = c(1,3,5,7,3,9)
> z = c(“A”, “B”, “C”, “D”, “E”, “C”)
> xyz = data.frame(z, x, y)
> saveRDS(xyz, “x.rds”)
> load(“x.rds”)
Error in load(“x.rds”) :
bad restore file magic number (file may be corrupted) — no data loaded

In this example, we saved with the saveRDS function, but try to load with the load function, resulting in our error message.

> x = c(1:5,3)
> y = c(1,3,5,7,3,9)
> z = c(“A”, “B”, “C”, “D”, “E”, “C”)
> xyz = data.frame(z, x, y)
> save(xyz, x, file = “x.rdata”)
> load(“x.rdata”)

In this example, we used the save and load functions properly and did not have any problems.

> x = c(1:5,3)
> y = c(1,3,5,7,3,9)
> z = c(“A”, “B”, “C”, “D”, “E”, “C”)
> xyz = data.frame(z, x, y)
> save.image(“xyz.rdata”)
> load(“xyz.rdata”)

In this example, we used the save.image and load functions to save all of the current r objects in use. This was proper and did not have any problems.

How to fix the R error

Here is an example, which fixes the error caused by mismatching save and load functions.

> x = c(1:5,3)
> y = c(1,3,5,7,3,9)
> z = c(“A”, “B”, “C”, “D”, “E”, “C”)
> xyz = data.frame(z, x, y)
> saveRDS(xyz, “x.rds”)
> readRDS(“x.rds”)
z x y
1 A 1 1
2 B 2 3
3 C 3 5
4 D 4 7
5 E 5 3
6 C 3 9

In this example, the saveRDS and readRDS functions are properly used together, and it fixes the error.

When saving files, it is important to make sure that you load them with the function that fits the one you used when saving them. If you get these formats mixed up you will get error messages. However, these problems are easy to fix. All you need to do is make sure that you are using the right set of functions. This is an easy mistake to make, but it is also an easy one to fix and avoid once you are aware of it.

Scroll to top
Privacy Policy