简体   繁体   中英

Trouble opening a tempfile in R

I am trying and failing to use a tempfile() to get data from a .gz file posted on the web without writing the archive to my hard drive and manually extracting the desired file. I'm re-using code that has worked in similar situations before, and R can find other tempfiles with no trouble.

Here's the code I'm using:

temp <- tempfile()
download.file("http://unified-democracy-scores.org/files/20140312/z/uds_summary.csv.gz", temp)
UDS <- read.csv(unz(temp, "uds_summary.csv"), stringsAsFactors = FALSE)

Here's the error it's throwing:

Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
  cannot open zip file 'C:\Users\Jay\AppData\Local\Temp\RtmpKs4ZWm\file100877485507'

I tried setting the mode in download.file() to other options (eg, mode="wb" ) to no avail. Ditto for varying the method at that step. If I download the archive to my hard drive and manually extract the .csv using the name used in the third line of my code, it reads in fine.

Any ideas what I'm doing wrong here?

Use gzfile instead of unz :

UDS <- read.csv(gzfile(temp), stringsAsFactors = FALSE)

This gives the output:

head(UDS)
#>         country year cowcode     mean        sd   median    pct025
#> 1 United States 1946       2 1.086431 0.2962744 1.072743 0.5424734
#> 2 United States 1947       2 1.094423 0.2989538 1.077987 0.5516301
#> 3 United States 1948       2 1.050040 0.2604016 1.038927 0.5642550
#> 4 United States 1949       2 1.039801 0.2585845 1.031048 0.5628056
#> 5 United States 1950       2 1.084971 0.2449264 1.071610 0.6280569
#> 6 United States 1951       2 1.043591 0.2551857 1.033722 0.5695530
#>     pct975
#> 1 1.694063
#> 2 1.719771
#> 3 1.588783
#> 4 1.567912
#> 5 1.589253
#> 6 1.577150

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM