0

I am trying to read rds file, directly from GitHub. I am able to read any file from git but when I try to read rds file using gzcon its asking value for con.

dat <- readRDS(gzcon(url("http://mgimond.github.io/ES218/Data/ABC.rds")))

exception : con has not defined. what type of connection it requires?

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Kalpesh
  • 694
  • 2
  • 8
  • 28

2 Answers2

3

I have found this to work and to be much simpler than using a temporary file:

u <- "https://mgimond.github.io/ES218/Data/ACS.rds"
data <- readRDS(url(u, method="libcurl"))

You can see this has properly read in the data since:

> names(data)
 [1] "County"     "State"      "B19013001"  "B19013001s" "B23006001" 
 [6] "B23006002"  "B23006003"  "B23006004"  "B23006005"  "B23006006" 
[11] "B23006007"  "B23006008"  "B23006009"  "B23006010"  "B23006011" 
[16] "B23006012"  "B23006013"  "B23006014"  "B23006015"  "B23006016" 
[21] "B23006017"  "B23006018"  "B23006019"  "B23006020"  "B23006021" 
[26] "B23006022"  "B23006023"  "B23006024"  "B23006025"  "B23006026" 
[31] "B23006027"  "B23006028"  "B23006029"  "B23006001s" "B23006002s"
[36] "B23006003s" "B23006004s" "B23006005s" "B23006006s" "B23006007s"
[41] "B23006008s" "B23006009s" "B23006010s" "B23006011s" "B23006012s"
[46] "B23006013s" "B23006014s" "B23006015s" "B23006016s" "B23006017s"
[51] "B23006018s" "B23006019s" "B23006020s" "B23006021s" "B23006022s"
[56] "B23006023s" "B23006024s" "B23006025s" "B23006026s" "B23006027s"
[61] "B23006028s" "B23006029s"

I do see that this seems to be the American Community Survey. The Census has an API that allows you to access this data directly through their API. You may want to explore that here: https://www.census.gov/data/developers/data-sets.html.

The tidycensus package in R offers direct access to the American Community Survey via the Census API as well. For example, after registering for a Census API key, the following code would allow you to access the 2017 ACS-1 data from Table DP05:

library(readr)
library(tidyverse)
library(tidycensus)

#Get census API key at:  https://api.census.gov/data/key_signup.html
census_api_key("insert your API key here")

#Get Age Data from 2017 ACS-1 Survey; includes DC and Puerto Rico
DP052017 <- get_acs(geography = "state", year = 2017, table = "DP05", 
                    cache_table = TRUE, survey = "acs1")

head(DP052017)

head(DP052017)
# A tibble: 6 x 5
  GEOID NAME    variable    estimate    moe
  <chr> <chr>   <chr>          <dbl>  <dbl>
1 01    Alabama DP05_0001  4874747     NA  
2 01    Alabama DP05_0001P 4874747     NA  
3 01    Alabama DP05_0002  2359896   5397  
4 01    Alabama DP05_0002P      48.4    0.1
5 01    Alabama DP05_0003  2514851   5397  
6 01    Alabama DP05_0003P      51.6    0.1
guyabel
  • 8,014
  • 6
  • 57
  • 86
StatsStudent
  • 1,384
  • 2
  • 10
  • 28
1

If you are having issues one way is to download the file as a tempfile.

url <- "mgimond.github.io/ES218/Data/ACS.rds"
temp <- tempfile() # create a tempfile
download.file(url, temp) # download to disk
dat <- readRDS(temp) # read the tempfile
unlink(temp) # Deletes tempfile

This should get you close!

MDEWITT
  • 2,338
  • 2
  • 12
  • 23
  • now its giving this error : Error in readRDS(temp) : embedded nul in string: 'name\0' – Kalpesh Mar 04 '19 at 20:26
  • Sounds like your file has some weird encodings. Couple of options...try `readr::read_rds(temp)`. additionally, check out this post for more details on the encoding issue. I don't know how to fix it in readRDS. IF that doesn't work let me know and you might have to try connections rather than readRDS – MDEWITT Mar 05 '19 at 14:34