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.

 

 

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 the Statistics Globe Newsletter

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


7 Comments. Leave new

  • how to load ggplot2 in R studio? In packages i can see ggplot2 however in console ggplot2 shows not found.
    Error in load(“C:/Users/Rupali/AppData/Local/Temp/RtmpSa4tkN/downloaded_packages/ggplot2_3.3.5.zip”) :
    bad restore file magic number (file may be corrupted) — no data loaded
    In addition: Warning message:
    file ‘ggplot2_3.3.5.zip’ has magic number ‘PK’
    Use of save versions prior to 2 is deprecated
    this is the message displayed. can you help?

    Reply
  • Hi Joachim,

    Thank you for your explanation.

    I can read my rds file successfully using readRDS, however, when I try to read the object, there is no object.
    Should I use anything else in the readRDS command?

    Thank you,

    Carlos

    Reply
  • Hello! Thank you for your page.

    Do you have any advice for reading in an RDA file? When I tried to click open the file, R studio automatically opened with load(“filename.rda”) and it gave me the error of Error in load(“filename.rda”) :
    bad restore file magic number (file may be corrupted) — no data loaded.

    I have since tried to use source() and readRDS() to open the file, but these do not work either. Thank you so much for your help!

    Reply
    • Hey Isa,

      Thank you for your kind comment! I would also use the load() function to import RDA files, and it’s surprising that your code didn’t work. Is it possible that the issue is with the RDA file itself? It might be corrupted or damaged.

      Regards,
      Joachim

      Reply

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.

Top