Import data into R (rdata) from Github

I want to add R code plus the associated data file (RData) in Github.

So far, everything is working fine. But when people clone the repository, I want them to be able to run the code right away. This is not possible at the moment, because they will need to change their working directory (setwd) to the directory that cloned the RData file (say).

So I thought it might be easier if I changed the R code so that it was linked to the RData file on github. But I can't get this to work using the following snippet. I think there may be a problem with the text / binary problem.

x <- RCurl::getURL("https://github.com/thefactmachine/hex-binning-gis-data/raw/master/popDensity.RData") y <- load(x) 

Any help would be appreciated.

thanks

+6
source share
3 answers

This works for me:

 githubURL <- "https://github.com/thefactmachine/hex-binning-gis-data/raw/master/popDensity.RData" load(url(githubURL)) head(df) # XYZ # 1 16602794 -4183983 94.92019 # 2 16602814 -4183983 91.15794 # 3 16602834 -4183983 87.44995 # 4 16602854 -4183983 83.79617 # 5 16602874 -4183983 80.19643 # 6 16602894 -4183983 76.65052 

EDIT Reply to OP comment.

From the documentation:

Note that the https: // URL scheme is not supported except Windows.

So you can try the following:

 download.file(githubURL,"myfile") load("myfile") 

which works for me too, but it clutters your working directory. If this does not work, try setting method="curl" in the call to download.file(...) .

+5
source

load accepts the file name.

 x <- getURL("https://github.com/thefactmachine/hex-binning-gis-data/raw/master/popDensity.RData") writeLines(x, tmp <- tempfile()) y <- load(tmp) 
0
source

I also had problems with this before, and the solution that I consider the most reliable is to use the tiny modification of source_url from the fantastic package [devtools] [1]. This works for me (on Mac).

 load_url <- function (url, ..., sha1 = NULL) { # based very closely on code for devtools::source_url stopifnot(is.character(url), length(url) == 1) temp_file <- tempfile() on.exit(unlink(temp_file)) request <- httr::GET(url) httr::stop_for_status(request) writeBin(httr::content(request, type = "raw"), temp_file) file_sha1 <- digest::digest(file = temp_file, algo = "sha1") if (is.null(sha1)) { message("SHA-1 hash of file is ", file_sha1) } else { if (nchar(sha1) < 6) { stop("Supplied SHA-1 hash is too short (must be at least 6 characters)") } file_sha1 <- substr(file_sha1, 1, nchar(sha1)) if (!identical(file_sha1, sha1)) { stop("SHA-1 hash of downloaded file (", file_sha1, ")\n does not match expected value (", sha1, ")", call. = FALSE) } } load(temp_file, envir = .GlobalEnv) } 

I use a very similar change to get text files from github using read.table etc. Please note that you need to use the "raw" version of the github url (which you included in your question).

[1] https://github.com/hadley/devtoolspackage

0
source

Source: https://habr.com/ru/post/972515/


All Articles