31

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

2
  • I had installed R 3.4 and noticed that this error always occurs. Surprisingly, installing R 3.3 instead of 3.4 fixed the issue for me. (What could be the reason for this?)
    – nikpod
    Nov 8, 2017 at 8:45
  • @nikpod if you saved the file in 3.3 maybe you can't read it in 3.4
    – Zach
    Nov 9, 2017 at 14:55

14 Answers 14

27

These are suggestions I have come across:

  1. Delete your .Rhistory and .RData files in the directory in which you are running R.
  2. Run update.packages()
  3. Try and detect "bad files" in your library directories. You can do this in R

    # List the library paths
    # The issue is likely to be in the first directory
    paths = .libPaths()
    
    ## Try and detect bad files
    list.files(paths, 
           pattern = "^00LOCK*|*\\.rds$|*\\.RDS$",
           full.names = TRUE)
    
    ## List files of size 0
    l = list.files(paths, full.names = TRUE)
    l[sapply(l, file.size) == 0]
    

    Delete any files/directories highlighted. You could use file.remove() if you really wanted to.

  4. Delete the directory in which you have stored your downloaded packages.

Only solution 3 worked for me.

Ref:

  • R-sig-Debian mailing list
  • Option 3 was a combination of answers provided by different people over the last few years, including Chunxiao Xu, Larry Hunsicker and Frank Harrell
6
  • 2
    same story. Had to switch for another directory Sep 30, 2015 at 13:53
  • 2
    I have tried all the above solutions and none of them seem to work. I couldn't find the .Rhistory and Rdata files.
    – Parsa
    Oct 17, 2017 at 14:56
  • 1
    @amc Agreed. I've created a simple R solution to try and detect bad files/directories Jun 19, 2018 at 21:48
  • Yeah for me it had to do with 1 offending library that had an rds file within it that was built by a newer R. I manually tried deleting dozens of libraries before finding the one. I wish I had found this solution first!
    – mkirzon
    Sep 25, 2020 at 14:07
  • I run the code in solution 3, but it only returns the paths in the directory, i.e. i.stack.imgur.com/MLxdw.png. Could you please explain how to use file.remove() to automatically remove file with size 0?
    – Akira
    Jan 15, 2021 at 9:42
7

Run find /usr/local/lib/R/site-library/ /usr/lib/R/library/ /usr/lib/R/site-library/ ~/.local/lib/ -iname '*rds' -a -size 0 and then delete the files found.

2
  • This did the trick (without needing to delete the entire package directory). I also had to delete a package lock file (/usr/local/lib/R/site-library/00LOCK-pkg). After deleting the files I reinstalled the affected packages.
    – amc
    Jun 19, 2018 at 15:07
  • I love you. This worked for me and I was not enjoying having to wrangle with R in order to fix R.
    – bennlich
    Sep 10, 2019 at 0:07
5

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

3

Something simple to try: if it is an .rda file use load instead of readRDS. You can then save the loaded file as an .rds and try readRDS again.

1
  • Related to this. I fixed it by downloading again the file in question from internet. Seems there was a small corruption and the error came up
    – JRodrigoF
    Apr 12, 2020 at 1:36
3

Here is what I had to do to solve this error:

  • Open your Rstudio, go to install tab under packages
  • Note your install to library path.(where your package will be saved)
  • Go to the install library path.And hard delete the recent packages
  • Refresh you package details in Rstudio
2

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.

0

You might have some problem with R packages . See if the problem has occurred after you installed some packed or did you copied the packages.

What you can do is Copy all the packages from Library and save it somewhere, then Copy some packages on the Library and restart the R test it and see how it works you have to try with all the packages one by one . I has the same issue and it was due to package problem.

0

I also had several problems opening an .RDS file, either "error reading from connection" or "unknown input format". I tried with several R versions, with version 3.4.2. the following worked: (I accidentally uninstalled rstudio and installed it again, also installed older R version 3.4.2, I don't know if that changes anything)

  1. set up a new working directory
  2. I opened the RDS file directly instead of downloading it (so only saved to temp files), then I was able to open it in R
  3. I moved it from temp files to my new working directory and used the Data_Essay_Account <- readRDS("C:/.../newdirectory/Data_Essay_Account.RDS") command and it worked.

Somehow properly downloading it and then moving it did not work, only after I had it in the temp files. Hard deleting packages did not work for me as I tried that initially.

1
  • Please use formatting tools to properly edit and format your question/answer. Codes within sentences are to be formatted as code Very Important words to be bold , lesser important onces Italic Also use lists if necessary
    – Morse
    Apr 1, 2018 at 23:08
0

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.

0

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.

0

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"]
0

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.

0

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)
})
0

The workaround

Execute this command in the R console:

options(pkgType = "source")

You're good to go.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.