14

I am trying to use an R script hosted on GitHub plugin-draw.R. How should I use this plugin?

Kim
  • 4,080
  • 2
  • 30
  • 51
user26480
  • 367
  • 2
  • 5
  • 15

4 Answers4

13

You can simply use source_url from package devtools, using the url you get by clicking on raw on the files 's Github page:

library(devtools)
source_url("https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/bingSearchXScraper/bingSearchXScraper.R")

Edit: use simply source()

Since recent versions of R, it seems you can simply use source() with the same "raw" url:

source("https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/bingSearchXScraper/bingSearchXScraper.R")
ls()
#> [1] "bingSearchXScraper"
Matifou
  • 7,968
  • 3
  • 47
  • 52
  • this basically works but it seems that the e.g. the roxygen comments are not fetched. E.g. when running `source("https://raw.githubusercontent.com/3dgeo-heidelberg/pytreedb/224339bc03918aadeed4ca1345ce9feeb312536f/examples_api/example.R")` – jens wiesehahn Jun 02 '23 at 10:13
7

You can use solution offered on R-Bloggers:

source_github <- function(u) {
  # load package
  require(RCurl)
 
  # read script lines from website
  script <- getURL(u, ssl.verifypeer = FALSE)
 
  # parase lines and evaluate in the global environment
  eval(parse(text = script))
}
 
source_github("https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/bingSearchXScraper/bingSearchXScraper.R")

For the function to be evaluated in a global environment (I'm guessing that you will prefer this solution) you can use:

source_https <- function(u, unlink.tmp.certs = FALSE) {
  # load package
  require(RCurl)
 
  # read script lines from website using a security certificate
  if(!file.exists("cacert.pem")) download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile = "cacert.pem")
  script <- getURL(u, followlocation = TRUE, cainfo = "cacert.pem")
  if(unlink.tmp.certs) unlink("cacert.pem")
 
  # parase lines and evealuate in the global environement
  eval(parse(text = script), envir= .GlobalEnv)
}
 
source_https("https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/bingSearchXScraper/bingSearchXScraper.R")
source_https("https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/htmlToText/htmlToText.R", unlink.tmp.certs = TRUE)

As mentioned in the the original article by Tony Breyal, this discussion on SO should also be credited as it is relevant to the discussed question.

saQuist
  • 416
  • 7
  • 19
Konrad
  • 17,740
  • 16
  • 106
  • 167
7

Based on @Matifou's reply, but using the "new" method appending ?raw=TRUE at the end of your URL:

devtools::source_url("https://github.com/tonybreyal/Blog-Reference-Functions/blob/master/R/bingSearchXScraper/bingSearchXScraper.R?raw=TRUE")
brunj7
  • 149
  • 1
  • 8
  • 3
    This works in that when you paste it into a browser, it will redirect you to a url with the token included, but it still gives me a 404 when I try to use it with `devtools::source_url`. – Dannid May 30 '19 at 18:15
6

If it is a link on GitHub where you can click on Raw next to the Blame, you can actually just use the ordinary base::source. Go to the R script of your choice and find the Raw button.

enter image description here

The link will contain raw.githubusercontent.com now, and the page show nothing but R script itself. Then, for this example,

source(
  paste0(
    "https://raw.githubusercontent.com/betanalpha/knitr_case_studies/master/", 
    "stan_intro/stan_utility.R"
  )
)

(paste0 was used just to fit the URL into a narrower screen.)

Kim
  • 4,080
  • 2
  • 30
  • 51