Installing R Packages Error in readRDS(file) : err

2020-03-01 03:38发布

问题:

Whenever I try to install any package in R on Ubuntu 14.04, I'm getting the following error:

Error in readRDS(file) : error reading from connection

I already tried the methods given here but could not solve the problem.

回答1:

1- Install the latest version of R form the CRAN and try to install a package.

2- If it is possible check it with another user account.

3- Try to install the R package locally.

4- If there is an RDS file created by the old version of R you may have another sort of problem, this is the warning from R help:

Warning

These functions have provided a stable interface since R 2.4.0 (when the storage of serialized objects was changed from character to raw vectors). However, the serialization format may change in future versions of R, so this interface should not be used for long-term storage of R objects.

On 32-bit platforms a raw vector is limited to 2^31 - 1 bytes, but R objects can exceed this and their serializations will normally be larger than the objects.

Ref: help(serialize)



回答2:

I was facing the same error and I restarted R session, it worked for me.



回答3:

If you have one or more incorrectly installed packages (e.g. because you had to force-reboot during installation) you need to re-install this/these package(s). You can find them using this code:

library(purrr)

.libPaths() %>%
set_names() %>%
map(function(lib) {
    .packages(all.available = TRUE, lib.loc = lib) %>%
    keep(function(pkg) {
        f <- system.file('Meta', 'package.rds', package = pkg, lib.loc = lib)
        tryCatch({readRDS(f); FALSE}, error = function(e) TRUE)
    })
})

This will return a nested list containing the broken packages:

$`/home/yourname/R`
[1] "brokenpkg"

$`/usr/lib64/R/library`
character(0)

$`/usr/share/R/library`
character(0)

You might need to delete the directories 00LOCK-<pkgname> that R created in the library location while trying to install the packages.



回答4:

I had this error on Windows 10 after installing R 3.4.0 from 3.3.1 (all 64-bit). It was resolved by manually installing an unrelated package from CRAN (I used ggplot2). No idea what the root cause was, but perhaps this will work for you as well.

Output from my code:

> library(pacman)
> p_load(plyr, XLConnect, ggplot2, stringr, magrittr, kirkegaard, lubridate, weights, psych, psychometric, polycor, effsize, readr)
Installing package into ‘C:/Users/Emil/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
Error in install.packages : error reading from connection
 Error in loadNamespace(name) : there is no package called ‘BiocInstaller’ 

Then I restarted R, and ran the same code:

> library(pacman)
> p_load(plyr, XLConnect, ggplot2, stringr, magrittr, kirkegaard, lubridate, weights, psych, psychometric, polycor, effsize, readr)
Installing package into ‘C:/Users/Emil/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
Error in readRDS(dest) : error reading from connection

I.e. same code, different error. Odd. Then I restarted R again and installed a random package, then reran my code and it worked.

> install.packages("ggplot2")
Warning in install.packages :
  cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/src/contrib/PACKAGES.rds': HTTP status was '404 Not Found'
Installing package into ‘C:/Users/Emil/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
Warning in install.packages :
  cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/3.4/PACKAGES.rds': HTTP status was '404 Not Found'
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/ggplot2_2.2.1.zip'
Content type 'application/zip' length 2782171 bytes (2.7 MB)
downloaded 2.7 MB

package ‘ggplot2’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\Emil\AppData\Local\Temp\RtmpCq4cFX\downloaded_packages
> library(pacman)
> p_load(plyr, XLConnect, ggplot2, stringr, magrittr, kirkegaard, lubridate, weights, psych, psychometric, polycor, effsize, readr)
Installing package into ‘C:/Users/Emil/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/effsize_0.7.1.zip'
Content type 'application/zip' length 36713 bytes (35 KB)
downloaded 35 KB

package ‘effsize’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\Emil\AppData\Local\Temp\RtmpCq4cFX\downloaded_packages

effsize installed

So, error seems to have had something to do with pacman trying to install effsize.



回答5:

I also had the same problem. I followed the instructions given here http://www.ryantmoore.org/files/ht/htrtargz.pdf and installed all the required dependencies separately as and when they were required.



回答6:

I was getting error running install.packages("mice")

  1. I tried everything suggested by user1436187.
  2. After this I tried running update.packages(). received same error. I also got an error message where it was unable to run some command due to permissions.
  3. I closed my current session of R and restarted it again as an administrator.
  4. Ran command which was giving error earlier install.packages("mice")

This worked for me.



回答7:

I was facing the same error when i installed most recent version of R. many a times most recent version is not stable( for me, it was 3.4.2 on 08/11/2017). I uninstalled it and installed 3.4.1( earlier stable version), now there is no issue.



回答8:

I had the same problem:

readRDS(file) : error reading from connection.I did follow:

I found file.rds in folder Downloads, then made copy of file and put in another folder. And then I chose directory in:

R Session->Set working Directory->Choose directory->my new folder

After this action it works

And one interesting thing. When I copied the file downloaded by the function download.file("http://..../file.rds", "file.rds") and put the file in folder-directory, the problem remained. But when I copied the link http://....../file.rds and pasted it into the address bar, the file was downloaded in the folder Downloads on my computer from which I copied it and moved to the directory -folder. So I did not download file by the function of R download.file, copied file from Downloads and put it in the folder-directory. In this case, it works



回答9:

Make sure you are saving the rds file in correct format :

saveRDS(model, "path/file.rds")

Then read the .rds file using

model <- readRDS("path/file.rds")