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

后端 未结 9 1228
自闭症患者
自闭症患者 2020-11-29 05:54

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 m         


        
相关标签:
9条回答
  • 2020-11-29 06:02

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

    object <- readRDS("object.rds")
    
    0 讨论(0)
  • 2020-11-29 06:07

    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.

    0 讨论(0)
  • 2020-11-29 06:11

    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")
    
    0 讨论(0)
  • 2020-11-29 06:19

    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.

    0 讨论(0)
  • 2020-11-29 06:19

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

    0 讨论(0)
  • 2020-11-29 06:21

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

    0 讨论(0)
提交回复
热议问题