jalapic
jalapic

Reputation: 14212

Loading csv file from Private GitHub repo using httr

I want to download a csv file from a private GitHub repo and load it to the current environment. I don't want to simply read_csv("raw_git_url") as the script will be public and copy/pasting the raw_git_url will leave me with filename.csv?token=AHh_HT3Aetc.... and I don't want my token number being public.

Instead, I have got a GitHub authentication token and put it as a .Renviron file into my working directory.

I can e.g. write the file to disk/working-directory like this:

library(httr)
url <- "https://raw.githubusercontent.com/username/privatereponame/master/dataname.csv"
GET(url, write_disk("mynew.csv", overwrite=TRUE))

but I don't want to write a copy - just have the csv file available in the current environment. Am I missing something obvious? Changing write_disk.... to a readr::read_csv command isn't working.

I can do a hacky thing - access the raw content like this:

x=GET(url, authenticate(Sys.getenv("GITHUB_PAT1"), ""))
enc<-stringi::stri_enc_detect(content(x, "raw"))
content(x, "text", encoding = enc[[1]]$Encoding[1])

...and then writing some script to convert text to csv.... but this feels too long-winded.

Upvotes: 3

Views: 1424

Answers (1)

jalapic
jalapic

Reputation: 14212

sometimes you have to ask a question to dig to the answer:

x=GET(url, authenticate(Sys.getenv("GITHUB_PAT1"), ""))
content(x, type="text/csv")

Using the type argument in content() sets the MIME type.

Upvotes: 5

Related Questions