Error in readRDS("<Rds file name here>") : error reading from connection

Howdy,
I'm looking to use this Shiny - Google Charts as an example and start to dig into how I can use my data with a display like this bubble graph with the slider.

I've copied the code and downloaded the RDS file. I even got it all running yesterday but now today I get this error:

Error in readRDS("healthexp.Rds") : error reading from connection

I've;

checked spelling (all fine)
checked that WD is correctly set
run another shiny app locally (all fine)
updated R to the latest version for MAC 1.1.453
searched the internet with no luck

Thoughts?

It's possible your working directory has changed; use getwd() to see what it is. If it's different than you expect, you can use setwd() to change it to the directory that healthexp.Rds is in. Or better yet, use RStudio projects and never mess with your working directory--instead copy the assets you need (like the .Rds file) into the project directory.

2 Likes

WD was the first thing I thought of as well, no luck there. I've even tried starting over in a new folder. No luck with any of that.

I created a project and copied files into folder, still no luck.

Any other thoughts appreciated?

The error message error reading from connection is most commonly an issue with one's filepath.

When I am confused by where an RDS files is relative to my working directory, I use RStudio's file pane to navigate to the offending file, click on it and open. The R console then reveals it's location.

Compare that to the current working directory getwd()

Yep, done a lot of playing around as noted with path. None of these path descriptions work. Same error with each.

Notes: R Project gives same issue, restarting R no resolution, starting over copying files from web in R project or just normal directory no luck either.

What happens when you load the rds vis the Load R Object gui? (You console doesn't show the attempt yet)

same error.

healthexp <- readRDS("~/Data/shinny/googlebubble71818/healthexp.Rds")
Error in readRDS("~/Data/shinny/googlebubble71818/healthexp.Rds") :
error reading from connection

Interestingly I pushed this to shiny.io and it has same error.
https://markloessi.shinyapps.io/googlechartsProj/

Something wrong with the file ? maybe.

Tried installing rio to import RDS but same error :frowning:

So, I downloaded the RDS file again from JCheng GitHub account and that fixed the issue.

For completion sake I did have to change %.% to %>% in this bit of code to get it to run in my environment (noted above).

df <- data %>%
  filter(Year == input$year) %>%
  select(Country, Health.Expenditure, Life.Expectancy,
         Region, Population) %>%
  arrange(Region)

Thanks for all the attempts to help, much appreciated!

Thought I might add my finding here in case people in the future encounter the same problem.

Ran into exact error with a slight twist just now. After crawling through this thread and some painstaking guess work, I found out that RDS downloaded from "download.file()" will return this error while RDS downloaded from browser does not.

download.file(path, destfile, mode = "wb") solves this issue. I guess the function added some weird headers to the rds file if you use mode = "w" because the file size (under mode="w") is slightly larger than it should. mode = "wb" tells the function that the rds file is a binary.

4 Likes

Another way of getting this error is if you are trying to read a file that was written by saving object with saveRDS, but the object didn't exist.

Silly, but can (and have) happen. :slight_smile:

1 Like