Peter Pearman
Peter Pearman

Reputation: 129

R download .RData

I want to download a .RData file from a development branch of a repo, as part of developing a setup chunk in a .Rmd file that implements a learnr tutorial. When I download the file manually from github, load("./data_download/elic_2016_1.RData")successfully loads the downloaded object. However, after downloading the .RData file with download.file(), the subsequent load() produces an error,

download.file("https://github.com/pbpearman/r-consortium-proposal/blob/interactive/material/lessons/switzerland-dual-use/data_clean/elic_2016_1.RData", destfile= "./data_download/elic_2016_1.RData", mode = "wb")
load("./data_download/elic_2016_1.RData")

file ‘elic_2016_1.RData’ has magic number ''

Use of save versions prior to 2 is deprecatedError in load("./data_download/elic_2016_1.RData")

bad restore file magic number (file may be corrupted) -- no data loaded

I also tried this, following stackoverflow.com/questions/26108575 :

load(url("https://github.com/pbpearman/r-consortium-proposal/blob/interactive/material/lessons/switzerland-dual-use/data_clean/elic_2016_1.RData"))

Error in load(url("https://github.com/pbpearman/r-consortium-proposal/blob/interactive/material/lessons/switzerland-dual-use/data_clean/elic_2016_1.RData")) : the input does not start with a magic number compatible with loading from a connection

I created the file with my currently installed R version. The file elic_2016_1.RData on Github doesn't appear to be corrupted because the manually downloaded file loads successfully. The same errors occur whether I run the code line-by-line within the chunk, or run each line from the command line. I tried zipping the .RData file and uploading it, but it still was corrupted upon downloading and unzipping.

What's going wrong and how can I download the .RData file from my repo and load it into R?

I am using R version 3.4.2 and RStudio 1.1.383.

Upvotes: 2

Views: 3141

Answers (2)

Scientist
Scientist

Reputation: 1339

Based on the answer by @clemens, the following input should work:

load(url("https://github.com/pbpearman/r-consortium-proposal/blob/interactive/material/lessons/switzerland-dual-use/data_clean/elic_2016_1.RData?raw=true"))

Upvotes: 0

clemens
clemens

Reputation: 6813

The problem is that you are downloading an HTML file from github. If you change the URL and add ''?raw=true', it will download the file:

url <- "https://github.com/pbpearman/r-consortium-proposal/blob/interactive/material/lessons/switzerland-dual-use/data_clean/elic_2016_1.RData?raw=true"

download.file(url, destfile= "./data_download/elic_2016_1.RData", mode = "wb")
load("./data_download/elic_2016_1.RData")

elic_2016_1
# A tibble: 3,083 x 8
   Quartal Geschäftsnummer Bestimmungsland                     Güterart
     <chr>           <dbl>           <chr>                        <chr>
 1   16/01         8007724         Ägypten               Dual Use Güter
 2   16/01         8007844         Ägypten               Dual Use Güter
 3   16/01         8007844         Ägypten               Dual Use Güter
 4   16/01         8007844         Ägypten               Dual Use Güter
 5   16/01         8006915         Ägypten               Dual Use Güter
 6   16/01         8006792         Ägypten National kontrollierte Güter
 7   16/01         8006792         Ägypten National kontrollierte Güter
 8   16/01         8006402         Ägypten               Dual Use Güter
 9   16/01         8006496         Ägypten               Dual Use Güter
10   16/01         8007768        Algerien               Dual Use Güter
# ... with 3,073 more rows, and 4 more variables: Geschäftstyp <chr>,
#   Richtung <chr>, `Exportkontrollnummer [EKN]` <chr>, `Wert [CHF]` <dbl>

Upvotes: 1

Related Questions