Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sourcing an R script from github, for global session use, from within a wrapper function?

Tags:

I can source an R script held on github (using the 'raw' text link) as follows:

# load package
require(RCurl)

# check 1
ls()
#character(0)

# read script lines from website
u <- "https://raw.github.com/tonybreyal/Blog-Reference-Functions/master/R/bingSearchXScraper/bingSearchXScraper.R"
script <- getURL(u, ssl.verifypeer = FALSE)
eval(parse(text = script))

# clean-up
rm("script", "u")

# check 2
ls()
#[1] "bingSearchXScraper"

However, what I would really like to do is wrap that up in a function. This is where I run into problems and I suspect it has something to do with the functions of the script only existing locally within the function it's called in. For example, here is the sort of thing I am aiming for:

source_github <- function(u) {
  # load package
  require(RCurl)

  # read script lines from website and evaluate
  script <- getURL(u, ssl.verifypeer = FALSE)
  eval(parse(text = script))
}  

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

Many thanks in advance for your time.

like image 695
Tony Breyal Avatar asked Nov 22 '11 16:11

Tony Breyal


People also ask

How do I save an R file from GitHub?

Create an R Markdown document in RStudio Now that you have a local copy of the repository, let's add an R Markdown document to your project. In RStudio click File , New File , R Markdown . Choose HTML output as default output. Click File , Save to save the document.


1 Answers

Use:

 eval(parse(text = script),envir=.GlobalEnv)

to stick the results into your default search space. Overwriting anything else with the same names, of course.

like image 113
Spacedman Avatar answered Sep 18 '22 20:09

Spacedman