Q:

Installing R Packages Error in readRDS(file) : error reading from connection

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.

A:

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)

A:

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

A:

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.

