Rcpp: Source Cpp code from online source

Created on 7 Mar 2018  ·  10Comments  ·  Source: RcppCore/Rcpp

Recently, there was an interesting StackOverflow question that came up regarding how to source code from GitHub. Sourcing code from an online repository has precedents in other _R_ packages. Most notably, shiny implements several ways to run an app from shiny::runGist() to shiny:: runUrl().

Dirk wrote a nice little five liner to solve this issue:

library(Rcpp)
remurl <- "https://github.com/slwu89/MCMC/blob/master/adaptMCMC_source.cpp"
locfile <- "/tmp/mcmc.cpp"
download.file(url=remurl, destfile=locfile)
sourceCpp(locfile)   # dozens of error for _this_ file

I think it may be helpful to wrap this up into three functions:

  • compileGist()
  • compileGitHub()
  • compileURL()

The pros of this approach:

  • Easier way to run code found online
  • Allows for examples to just "work" without copy paste errors.

The cons of this approach:

  • sourceCpp() has already diluted the need to learn how to _package_ compiled code. By adding this, there is a risk of further neglecting package builds.
  • This adds a new feature to the package.

Most helpful comment

I'd be open to extending sourceCpp() to understand http:// URLs (where we just attempt to download the file and source it for you) but would rather not add extra helper functions.

I'm also open to doing nothing at all and saying the onus is on the user to download the file explicitly before sourcing it.

I'd prefer not adding the extra compile*() APIs -- I think life is easier with just one entrypoint.

All 10 comments

Intend to close as very clearly stated over Slack.

I'd be open to extending sourceCpp() to understand http:// URLs (where we just attempt to download the file and source it for you) but would rather not add extra helper functions.

I'm also open to doing nothing at all and saying the onus is on the user to download the file explicitly before sourcing it.

I'd prefer not adding the extra compile*() APIs -- I think life is easier with just one entrypoint.

@kevinushey I'm more of a fan of modifying sourceCpp() as well. I proposed the helpers to match with prior work done in the _R_ ecosystem.

I think having the URL component would amplify the usefulness of the Gallery as code could be spun into its own cpp file and then sourced.

Quite a few mistakes arise on SO because of bad gallery copy + pastes.

The burden is on us for testing, consistence, easy of use.

It really looks like you badly want a mostly personaly helper function. I still suggest you write one for yourself. We don't really have to enlarge the package for this. (And yes, @kevinushey is current that we _could_ alter sourceCpp() to recognise a URL but again, needs testing, it already is a big and mighty function etc pp.).

As for the galltery argument: half the sources, give or take, are Rmd files.

@eddelbuettel I'm not a fan of _adding_ dependencies to _Rcpp_. The URL route should be of similar caliber to the 4-liner.

The "source" feature of the gallery isn't as apparent to some users. I've witnessed this a few times as it is a symptom of _functional fixedness_, or assuming the page is simply a blog. Again, I really think there could be a substantial benefit from adding a URL component.

The productivity of just _organizing_ files for an individual is a huge step to lower the computational barriers. c.f. use_course() via usethis for tutorial sessions.

https://github.com/r-lib/usethis/issues/132

If there really is no interest, I do not mind retracting the issue.

Nobody really spoke up, so in the interest of keeping the open issue count smaller, I'm closing this.

BTW, I did add this helper function at the SO question:

u2f <- function(url) { 
   tf <- tempfile() 
   download.file(url, tf, quiet=TRUE) 
   tf 
}
library(Rcpp)
url <- "https://raw.githubusercontent.com/slwu89/MCMC/master/adaptMCMC_source.cpp"
sourceCpp( u2f( url ) )

Make perfect sense in a personal helper function, or heck, maybe even on the help page for sourceCpp.

@eddelbuettel if you don't mind, I'd like to ping others as this issue on incorporating web compilation may have been lost in the fray.

Any thoughts on enabling sourceCpp() to accept a web URL? @jjallaire @nathan-russell @thirdwing

Current tally:

@eddelbuettel is on record w/ no.
@kevinushey is on record w/ a neutral stance.

Sorry but you had seven days for this. And I read Kevin as a nope here. No new entry points.

I think life is easier with just one entry point.

I agree with Kevin.

Also, bears repeating that

  • even the gallery half 1/2 Rmd files where it would not help
  • on the 1/2 of cpp files you still need to adjust the URL

It's an ok idea in theory, but in practice it isn't compelling enough.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

helmingstay picture helmingstay  ·  4Comments

sgsokol picture sgsokol  ·  7Comments

iago-pssjd picture iago-pssjd  ·  4Comments

karthik2527 picture karthik2527  ·  4Comments

steve-s picture steve-s  ·  4Comments