readRDS(file) in R

Viewed 54229

Whenever I try to install a package in R, I get the following error:

Error in readRDS(file) : unknown input format

This just started occurring after I had a system crash. I am running 32 bit R 2.13.0 under windows 7. I tried removing and re-installing R, but continue to get the error. Is there any way I can fix this without deleting everything (i.e. all the packages I've installed) and starting over?

Thanks

14 Answers

Chunxiao Xu and Rando Hinn's solution above worked for me, with a minor tweak.

First, change directories to your personal R package directory, then run: find -iname '*rds' -a -size 0

Delete the directories containing any 0 length files in the above list. Then reopen R (or RStudio) and reinstall the deleted packages.

You should now be able to list the packages again.

Chunxiao Xu's original suggestion above lists ALL of the R package directories. But the exact locations of these directories will vary from installation to installation, and their addresses will have to be edited for your installation.

Larry Hunsicker

This happened to me after I had to reboot my machine in the middle of installing several R packages. I removed the latest installed packages by physically removing the files in the appropriate library directory, reinstalled them, and all was well. I also had to remove a 00LOCK directory in the library directory.

I checked for this issue on several forums and then I tried this and it worked for me:

1) Remove the ggplot2 package first

remove.packages("ggplot2")

2) Then Install the package again

install.packages("ggplot2")

3) restart rstudio and install your package again, hope this helps

Seems that the issue was with the required package (ggplot2 in my case) got corrupt and was hampering the installation of any new dependent package.

Unlike the top few answers here, my issue was resolved not by doing anything with my installation of RStudio or cleaning local files, but by re-writing the RDS file.

I could confirm this because the file was stored in the cloud, and reading it from a variety of windows and mac laptops all failed, but similar RDS files from the same S3 bucket worked without a problem.

When I re-wrote the RDS file, the problem went away.

I had the same problem as the OP, but in my case the problem .rds files were not 0 bytes but had file size >0 and were full of NULL characters (at least that's what I saw when I opened them in Notepad++)

By using debugonce(loadNamespace) before loading the library that was giving me the error (in my example it was the VIM package) I eventually traced my problem to corrupted .rds files in the META folder of the forcats package, which was installed as a 5th generation import when I installed VIM

    (In the rstudio environment pane)
 __NamesSpacesLoading__    chr[1:5] "forcats" "haven" "rio" "car" "VIM"

I then fixed by VIM problem simply by removing and reinstalling forcats
Adapting the code in @csgillespie accepted answer, I could have found this by trying to read all the rds files as below

paths = .libPaths()

l <- list.files(paths, 
           pattern = "*\\.rds$",
           ignore.case = T,
           recursive = T,
           full.names = TRUE)

checkRDS <- function(file) {
  tryCatch({
    readRDS(file)
    "OK"
  },
  error = function(cond) {
    return("Error")
  })
} 

l[sapply(l,checkRDS)=="Error"]

I was able to fix it by using .libPaths() and finding the 4.1 folder (my version of R was 4.1, so may differ). From there I closed R studio and deleted all the folders for each package.

I reinstalled all packages and found the packages which wouldn't download through the error messages. When I located the packages that didn't download I installed them by using: install.packages("package", type = "binary").

A bit long but solved all my issues.

My mirror was misconfigured. My mirror is Oregon State University because that is what I am closest too. Here is a list of mirrors. Call this code before installing any packages. More information in ?options.

mirror <- "https://ftp.osuosl.org/pub/cran/"
local({
  r <- getOption("repos")
  r["CRAN"] <- mirror
  options(repos = r)
})

The workaround

Execute this command in the R console:

options(pkgType = "source")

You're good to go.

Related