28

Is there some way to source an R script from the web?

e.g. source('http://github.com/project/R/file.r')

Reason: I currently have a project that I'd like to make available for use but isn't ready to be packaged yet. So it would be great to give people a single file to source from the web (that will then source all the individual function files).

On closer inspection, the problem appears to be https. How would I source this file?

https://raw.github.com/hadley/stringr/master/R/c.r
ulidtko
  • 14,740
  • 10
  • 56
  • 88
Maiasaura
  • 32,226
  • 27
  • 104
  • 108

6 Answers6

33

You can use the source_url in the devtools library

library(devtools)
source_url('https://raw.github.com/hadley/stringr/master/R/c.r')

This is a wrapper for the RCurl method by @ROLO

Mat
  • 202,337
  • 40
  • 393
  • 406
Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87
  • devtools also handles gists really nicely in R: devtools::source_gist('11326436') – tim Apr 26 '14 at 17:45
26

Yes you can, try running this R tutorial:

source("http://www.mayin.org/ajayshah/KB/R/tutorial.R")

(Source)

Https is only supported on Windows, when R is started with the --internet2 command line option (see FAQ):

> source("https://pastebin.com/raw.php?i=zdBYP5Ft")
> test()
[1] "passed"

Without this option, or on linux, you will get the error "unsupported URL scheme". In that case resort to the solution suggested by @ulidtko, or:

Here is a way to do it using RCurl, which also supports https:

    library(RCurl)
    eval( expr = 
        parse( text = getURL("http://www.mayin.org/ajayshah/KB/R/tutorial.R",
                       ssl.verifypeer=FALSE) ))

(You can remove the ssl.verifypeer if the ssl certificate is valid)

ROLO
  • 4,183
  • 25
  • 41
  • What is your system and R version? I get "unsupported URI scheme" error. – ulidtko Oct 10 '11 at 16:41
  • Strange... I tried it with R 2.13.1 x64 and 2.13.2 x64 on Windows 7 x64. In console mode (R.exe) it does not work for both, in Rgui only for 2.13.1, and with 2.13.2 as console in RStudio it also works. I checked with (.packages()) and always only these 7 are loaded: "stats"/"graphics"/"grDevices"/"utils"/"datasets"/"methods"/"base" – ROLO Oct 10 '11 at 16:56
  • 2
    Found the solution: if R is started with the option --internet2 sourcing from https works, see also [link](http://cran.r-project.org/bin/windows/rw-FAQ.html#The-Internet-download-functions-fail_002e) – ROLO Oct 10 '11 at 17:12
  • 1
    Short update: the option --internet2 is windows only. Sourcing from https under Linux does not work (tested), so you would need a solution like _ulidtko_ posted here. – ROLO Oct 10 '11 at 18:15
  • 1
    Thanks for continuing to dig until you found the solution. Could you update your answer to reflect what you found re: the `--internet2` option? – Josh O'Brien Oct 10 '11 at 18:39
14

Yes, it is possible and worked for me right away.

R> source("http://pastebin.com/raw.php?i=zdBYP5Ft")
R> test()
[1] "passed"

Regarding the HTTPS part, it isn't supported by internal R code. However, R can use external utilities like wget or curl to fetch https:// URLs. One will need to write additional code to be able to source the files.

Sample code might be like this:

wget.and.source <- function(url) {
  fname <- tempfile()
  download.file(url, fname, method="wget")
  source(fname)
  unlink(fname)
}

There is a Windows-only solution too: start R with --internet2 commandline option. This will switch all the internet code in R to using IE, and consequently HTTPS will work.

ulidtko
  • 14,740
  • 10
  • 56
  • 88
2

Windows:

If Internet Explorer is configured to access the web using your organization's proxy, you can direct R to use these IE settings instead of the default R settings. This change can be made once by the following steps:

  1. Save your work and close all R sessions you may have open.

  2. Edit the following file. (Note: Your exact path will differ based on your R installation)

    C:\Program Files\R\R-2.15.2\etc\Rprofile.site

Open this "Rprofile.site" file in Notepad and add the following line on a new line at the end of the file:

utils::setInternet2(TRUE)

You may now open a new R session and retry your "source" command.

Linux alikes:

Use G. Grothendieck's suggestion. At the command prompt within R type:

source(pipe(paste("wget -O -", "https://github.com/enter/your/url/here.r")))

You may get an error saying:

cannot verify certificate - - - - Self-signed certificate encountered.

At this point it is up to you to decide whether you trust the person issuing the self-signed certificate and proceed or to stop.

If you decide to proceed, you can connect insecurely as follows:

source(pipe(paste("wget -O -", "https://github.com/enter/your/url.r", "--no-check-certificate")))

For more details, see the following:

See section 2.19

Similar questions here:

Community
  • 1
  • 1
Stan
  • 905
  • 9
  • 20
1

The methods here were giving me the following error from github:

OpenSSL: error:14077458:SSL routines:SSL23_GET_SERVER_HELLO:reason(1112)

I used the following function to resolve it:

github.download = function(url) {
  fname <- tempfile()
  system(sprintf("curl -3 %s > %s", url, fname))                                                                                                                                                                                                                                                                                                                          
  return(fname)
}
source(github.download('http://github.com/project/R/file.r'))

Hope that helps!

Damon Snyder
  • 1,362
  • 1
  • 11
  • 18
0

This is working for me on windows:

library(RCurl)
# load functions and scripts from github ----------------------------
fn1 <- getURL("https://raw.githubusercontent.com/SanjitNarwekar/Advanced-R-Programming/master/fn_factorial_loop.R", ssl.verifypeer = FALSE)
eval(parse(text = fn1))