readRDS(file) in R
Asked Answered
F

16

32

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

Folie answered 24/6, 2011 at 21:13 Comment(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?)Sanborn
@Sanborn if you saved the file in 3.3 maybe you can't read it in 3.4Folie
N
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
Natelson answered 3/7, 2013 at 13:4 Comment(6)
same story. Had to switch for another directoryFyke
I have tried all the above solutions and none of them seem to work. I couldn't find the .Rhistory and Rdata files.Robin
@amc Agreed. I've created a simple R solution to try and detect bad files/directoriesNatelson
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!Bloodmobile
I run the code in solution 3, but it only returns the paths in the directory, i.e. i.sstatic.net/MLxdw.png. Could you please explain how to use file.remove() to automatically remove file with size 0?Elias
Another solution that worked for me is that I changed it from .rds to a .RData file and read in the collection of workspace objectsContrapositive
T
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.

Thanhthank answered 6/7, 2016 at 7:44 Comment(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.Tannie
I love you. This worked for me and I was not enjoying having to wrangle with R in order to fix R.Positively
U
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

Unseam answered 22/10, 2016 at 9:15 Comment(0)
M
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.

Moraine answered 20/4, 2017 at 3:23 Comment(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 upAmoreta
R
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
Rummer answered 27/2, 2018 at 18:40 Comment(0)
E
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.

Estep answered 12/1, 2018 at 17:52 Comment(0)
M
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.

Metamorphism answered 27/8, 2017 at 9:26 Comment(0)
E
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.

Espinoza answered 17/10, 2017 at 20:24 Comment(0)
G
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.

Guttapercha answered 1/4, 2018 at 22:45 Comment(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 necessaryKiddush
B
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.

Baulk answered 9/10, 2019 at 6:18 Comment(0)
S
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"]
Shouse answered 5/11, 2021 at 11:47 Comment(0)
M
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.

Motivity answered 9/11, 2021 at 14:22 Comment(0)
T
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)
})
Toplevel answered 3/4, 2022 at 15:6 Comment(0)
A
0

The workaround

Execute this command in the R console:

options(pkgType = "source")

You're good to go.

Askins answered 5/5, 2022 at 4:47 Comment(0)
F
0

My issue was that I was saved the RDS object with the save() function and not the saveRDS() function.

# Doesn't work
a = 1
save(a, file="test.rds") 
b = readRDS("test.rds")
print(b)
# Works
a = 1
saveRDS(a, file="test.rds") 
b = readRDS("test.rds")
print(b)
Fireplace answered 31/3 at 3:1 Comment(0)
H
0

Hi~ THis may happends when the rds file is too big (like over 2Gbi). You can try simply use load function:load("Your file path")

Have answered 3/4 at 5:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.