How to read file.rar directly from website in R

牧云@^-^@ 提交于 2019-12-08 07:08:42

问题


I wanted to download a file that zipped in open-plaques-all-2017-06-19.rar, but failed to implement it in R. Please have a look at my code below

temp <- tempfile()

download.file("https://github.com/tuyenhavan/Statistics/blob/master/open-plaques-all-2017-06-19.rar", temp)

df<- fread(unzip(temp, files = "open-plaques-all-2017-06-19.csv"))
head(df)

回答1:


For these respective platforms/pkg managers you'll need:

  • deb: libarchive-dev (Debian, Ubuntu, etc)
  • rpm: libarchive-devel (Fedora, CentOS, RHEL)
  • csw: libarchive_dev (Solaris)
  • brew: libarchive (Mac OSX)

Windows folks will have precompiled binaries auto-downloaded for them.

Then do:

devtools::install_github("jimhester/archive") 

Here's one workflow. Now that the URL you specified was not correct/valid. You need to use the "raw" URL to get to the actual file.

library(archive)

tf1 <- tempfile(fileext = ".rar")
download.file("https://github.com/tuyenhavan/Statistics/blob/master/open-plaques-all-2017-06-19.rar?raw=true", tf1)

tf2 <- tempfile()
archive_extract(tf1, tf2)

list.files(tf2)
## [1] "open-plaques-all-2017-06-19.csv"

file.size(file.path(tf2, list.files(tf2)))
## [1] 26942816

xdf <- readr::read_csv(file.path(tf2, list.files(tf2)))
dplyr::glimpse(xdf)
## Observations: 38,436
## Variables: 27
## $ id                     <int> 29923, 42945, 42944, 42943, 42942, 42941, 42940, ...
## $ title                  <chr> "Jon Pertwee blue plaque", "Apsley Cherry-Garrard...
## $ inscription            <chr> "Jon Pertwee 1919-1996 Doctor Who 1970-1974", "Ap...
## $ latitude               <dbl> NA, NA, NA, NA, NA, NA, 54.14910, 45.76330, NA, 4...
## $ longitude              <dbl> NA, NA, NA, NA, NA, NA, -4.46938, 4.83157, NA, 4....
## $ country                <chr> "United Kingdom", "United Kingdom", "United Kingd...
## $ area                   <chr> "London", "Bedford", "Harlow", "Bozen", "Adro", "...
## $ address                <chr> "BBC Television Centre", "Lansdowne Road", "The W...
## $ erected                <int> NA, NA, NA, NA, NA, 2016, NA, NA, NA, NA, NA, NA,...
## $ main_photo             <chr> NA, "https://commons.wikimedia.org/wiki/Special:F...
## $ colour                 <chr> "blue", "blue", "blue", "brass", "brass", "brass"...
## $ organisations          <chr> "[]", "[]", "[\"Harlow Civic Society\"]", "[\"Gun...
## $ language               <chr> "English", "English", "English", "Italian", "Ital...
## $ series                 <chr> NA, NA, NA, "Stolpersteine Italiano", "Stolperste...
## $ series_ref             <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N...
## $ `geolocated?`          <chr> "false", "false", "false", "false", "false", "fal...
## $ `photographed?`        <chr> "false", "true", "false", "true", "true", "true",...
## $ number_of_subjects     <int> 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0...
## $ lead_subject_name      <chr> "Jon Pertwee", "Apsley Cherry-Garrard", NA, NA, "...
## $ lead_subject_born_in   <int> 1919, 1886, NA, NA, 1911, 1913, NA, 1888, 1832, 1...
## $ lead_subject_died_in   <int> 1996, 1959, NA, NA, 1945, 1945, NA, 1967, 1898, 1...
## $ lead_subject_type      <chr> "man", "man", NA, NA, "man", "man", NA, "man", "m...
## $ lead_subject_roles     <chr> "[\"Doctor Who\", \"actor\", \"entertainer\", \"t...
## $ lead_subject_wikipedia <chr> "https://en.wikipedia.org/wiki/Jon_Pertwee", "htt...
## $ lead_subject_dbpedia   <chr> "http://dbpedia.org/resource/Jon_Pertwee", "http:...
## $ lead_subject_image     <chr> "https://commons.wikimedia.org/wiki/Special:FileP...
## $ subjects               <chr> "[\"Jon Pertwee|(1919-1996)|man|Doctor Who, actor...

Consider unlink()ing tf1, copying the file(s) from tf2 somewhere more permanent and then unlink()ing tf2 to clean up after the work is completed.




回答2:


I don't know if there is an R library for extracting RAR-archives, but if you have unrar, unar, p7zip or something similar installed you can call them through a system call and have them extract the file.
Also, you need to tag a ?raw=true to the end of the url to get the raw data (and not the html code).

This is using p7zip or unar on a mac, other utilities and systems might require different syntax.

temp <- tempfile()

download.file(paste0("https://github.com/tuyenhavan/Statistics/blob/master/", 
                    "open-plaques-all-2017-06-19.rar?raw=true"), temp)

#list all csv-files in current working directory
csv_files <- list.files(pattern="\\.csv")

#extract RAR to current working directory using p7zip
system(paste("7z x", temp, paste0("-o", getwd())))

#extract RAR to current working directory using unar
system(paste("unar", "-f", "-o", shQuote(getwd()), shQuote(temp)))

#find the name of the extracted csv file
csv_new <- setdiff(list.files(pattern="\\.csv"), csv_files)

#read in the csv as a data.frame
csv.dtf <- read.csv(csv_new)

You can also read in the csv directly, but it's fairly slow.

csv <- system(paste("7z x -so", temp), intern=TRUE)
csv.dtf <- read.csv(text=csv)


来源:https://stackoverflow.com/questions/46628844/how-to-read-file-rar-directly-from-website-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!