Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an R script from GitHub?

Tags:

github

r

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

like image 591
user26480 Avatar asked Mar 01 '16 10:03

user26480


2 Answers

You can simply use source_url from package devtools :

library(devtools)
source_url("https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/bingSearchXScraper/bingSearchXScraper.R")
like image 126
Matifou Avatar answered Sep 24 '22 16:09

Matifou


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.

like image 37
Konrad Avatar answered Sep 20 '22 16:09

Konrad