3

I wish to source some R script in a private github folder and make it available in some environment through R.

When using a public github folder, both of these commands do the job: get_URL and source_url (from devtools).

However, things get complicated when the folder is private and authentication is needed.

Is there a way to accomplish this and put login details within these functions that would pass the username & password automatically if some github autenthication is needed to go forward?

Thanks is advance,

Tamas

2 Answers2

5

TL;DR: It's possible, see the below code.


For anyone's future use, here's a method to source a R script from a private Github repo, using httr, and optionally, devtools.

I searched around enough to find various pieces to the solution, and just stitched them all together. So credit goes to multiple other threads and websites.

Some sources are here, here and here.

See below code:

library(httr)

# Source R script from Github
script <-
  GET(
    url = "https://api.github.com/repos/{user_name}/{repo_name}/contents/{script_name}.R",
    authenticate({github_email}, {github_personal_access_token}),     # Instead of PAT, could use password
    accept("application/vnd.github.v3.raw")
  ) %>%
  content(as = "text")

# Evaluate and parse to global environment
eval(parse(text = script))

This may only work for a private repo that you own. I'm not sure if it will work for a private repo that was shared with you.

See this link to create a Github Personal Access Token (PAT). You can also save this as an environment variable within R, if desired. devtools::github_pat() can be useful here.

Both your email and PAT (or password) need to be in quotations.

Ethan Wicker
  • 526
  • 7
  • 14
  • ```script <- GET( url = "https://api.github.com/repos/***/covid19/contents/01-wrangle-data-covid-ssa-mx-county.R", authenticate(Sys.getenv("GITHUB_PAT"), ""), # Instead of PAT, could use password accept("application/vnd.github.v3.raw") ) %>% content(as = "text") # Evaluate and parse to global environment eval(parse(text = script))```I am trying with this and this is my result ```Error in parse(text = script) : :2:25: unexpected ',' "message": "Not Found" ``` Do you know what is happening? – coding Jun 12 '20 at 03:29
  • Hi @coding. Without seeing more of your code, it's hard to tell. If I remember correctly (it's been over a year since writing the above), the `script` object above should contain an unparsed raw version of your R script. Basically, your R script, as an unformatted text object. The `eval()` and `parse()` functions just clean up this raw text object, and evaluate it to your R environment. From the error, it looks like your `script` object is not coming through as expected. If you exam it, does it look as expected? – Ethan Wicker Jun 14 '20 at 16:41
  • Similarly, perhaps the R script stored in Github isn't correct? The R script itself could be missing a comma or parentheses, and so `parse` can't parse it correctly. – Ethan Wicker Jun 14 '20 at 16:41
0

Thanks to Ethan and other sources I could solve this.

  1. Generate on GitHub your personal token
    1.1 Go to GitHub
    2.1 In the right corner go to "Settings"
    2.2 Then in the left part go to "Developer setting"
    2.3 Select the option "Personal access tokens"
    2.4 Select the option "Generate new token"
    2.5 Copy your personal token
  2. On your home directory follow the next steps
    2.1 Create the file .Renviron
macbook@user:~$ touch .Reviron

On this file write your personal token like this:

macbook@user:~$ nano .Reviron
GITHUB_PAT=YOUR PERSONAL TOKEN
  1. Now on R, you can check if your personal token has been saved with this:
Sys.getenv("GITHUB_PAT")

also you can edit your token on R with this:

usethis::edit_r_environ()

Don´t forget to restart R to save your changes.

3. Finally on R these are the line codes that will load your data from private repos

library(httr)

req <- content(GET(
  "https://api.github.com/repos/you_group/your_repository/contents/your_path_to your_doc/df_test.Rdata",
  add_headers(Authorization = "token YOUR_TOKEN")
), as = "parsed")

tmp <- tempfile()
r1 <- GET(req$download_url, write_disk(tmp))
load(tmp)

coding
  • 917
  • 2
  • 12
  • 25