Importing data into R (rdata) from Github

后端 未结 3 1707
后悔当初
后悔当初 2020-12-06 06:32

I want to put some R code plus the associated data file (RData) on Github.

So far, everything works okay. But when people clone the repository, I want them to be

3条回答
  •  孤城傲影
    2020-12-06 06:42

    I've had trouble with this before as well, and the solution I've found to be the most reliable is to use a tiny modification of source_url from the fantastic [devtools][1] package. This works for me (on a 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 modification to get text files from github using read.table, etc. 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

提交回复
热议问题