How to Fix the R Error: bad restore file magic number (file may be corrupted) — no data loaded

 

In this tutorial you’ll learn how to deal with the error message “bad restore file magic number (file may be corrupted) — no data loaded” in R.

Table of contents:

Let’s dive right into the examples.

 

Example 1: Reproduce the Error: bad restore file magic number (file may be corrupted) — no data loaded

This example shows how to replicate the error “bad restore file magic number (file may be corrupted) — no data loaded” in R.

First, we have to create some data:

x <- 5                     # Create some data

Furthermore, we have to export these data to an rds file using the saveRDS function:

saveRDS(x, "x.rds")        # Export data to rds object

Now, we may try to load these data back to RStudio using the load function:

load("x.rds")              # Trying to load data
# Error in load("x.rds") : 
#   bad restore file magic number (file may be corrupted) -- no data loaded
# In addition: Warning message:
# file 'x.rds' has magic number 'X'
#   Use of save versions prior to 2 is deprecated

Unfortunately, the error “bad restore file magic number (file may be corrupted) — no data loaded” was returned.

The reason for this is that the load function is not suited for rds files.

Next, I’ll show how to solve this problem.

 

Example 2: Fix the Error: bad restore file magic number (file may be corrupted) — no data loaded

In this example, I’ll show how to avoid the error message “bad restore file magic number (file may be corrupted) — no data loaded” when reading rds files to R.

We can properly load an rds file by using the readRDS function instead of the load function.

Have a look at the following R code:

readRDS("x.rds")           # Properly read rds file
# 5

No error!

 

Video & Further Resources

I have recently published a video on my YouTube channel, which illustrates the examples of this page. You can find the video tutorial below.

 

The YouTube video will be added soon.

 

Additionally, you could have a look at the other tutorials of my website. Some tutorials can be found below:

 

At this point you should have learned how to handle the error “bad restore file magic number (file may be corrupted) — no data loaded” in the R programming language. Don’t hesitate to let me know in the comments, in case you have any further questions and/or comments.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top