Importing data into R (rdata) from Github

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 able to run the code immediately. At the moment, this isn’t possible because they will have to change their work directory (setwd) to directory that the RData file was cloned (i.e. downloaded) to.

  • Can't push branch to github
  • Git tag in Visual Studio 2015 isn't visible on GitHub web site
  • How to upload html documentation generated from sphinx to github?
  • Tricking Git, slow internet Connection
  • GitHub Webhook Secret Never Validates
  • View commits that make changes to subfolder
  • Therefore, I thought it might be easier, if I changed the R code such that it linked to the RData file on github. But I cannot get this to work using the following snippet. I think perhaps there is some issue text / binary issue.

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

    Any help would be appreciated.

    Thanks

  • In Jenkins, how to checkout a project into a specific directory (using GIT)
  • git: I can add a directory, but at commit time I get “nothing to commit”
  • How to auto deploying git repositories with submodules on AWS?
  • Contributing to a core grunt task on github
  • How to find out which release(s) contain a given GIT commit?
  • How do I squash all commit history and push into another remote repository?
  • 3 Solutions collect form web for “Importing data into R (rdata) from Github”

    This works for me:

    githubURL <- "https://github.com/thefactmachine/hex-binning-gis-data/raw/master/popDensity.RData"
    load(url(githubURL))
    head(df)
    #          X        Y        Z
    # 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 Response to OP comment.

    From the documentation:

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

    So you could try this:

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

    which works for me as well, but this will clutter your working directory. If that doesn’t work, try setting method="curl" in the call to download.file(...).

    load takes a filename.

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

    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

    Git Baby is a git and github fan, let's start git clone.