ropensci / unconf18

http://unconf18.ropensci.org/
44 stars 4 forks source link

Compiling R logic to the browser #36

Open noamross opened 6 years ago

noamross commented 6 years ago

(A rambling post that might converge into an actual unconf project)

I've become intrigued lately with the applications of WebAssembly, the latest approach to running compiled code in the browser. I wrote up a proof-of-concept of how one could run the same C++ code via Rcpp or in an htmlwidget. I think some exploration of use cases and design patterns of shared R/browser implementations of algorithms in this way could make a good unconf project.

It's a huge project to try to port R runtime to the browser this way, but there are a number of R packages that generate C(++) code for fast model execution that could then be compiled to WebAssembly in the browser:

For any of these, one could imagine fitting and optimizing models in R, then porting them to the browser to power pages/apps built on simulation or prediction from those models. For instance, I could use odin to fit a set of ODEs to data, and then port the fit odin model to an htmlwidget that lets the user simulate and visualize those ODE systems, with the option of changing some parameters or initial conditions. Getting a Stan model to run in the browser in an htmlwidget seems super interesting, too - it would be like shinystan without Shiny. (One of the Stan devs has said they would be interested in helping with this.)

Alternatively, keeping the concept of compiling to the browser but in a very different way, we could compile dplyr code to the browser by using dbplyr to create SQL from R expressions, then sending this SQL to be executed by the browser via a JavaScript SQL implementation such as alasql. One could create "live" htmlwidgets this way: they could query a remote data source, transform the data, and then feed it to an output like a plot.ly graph or a DataTable, all in the browser with no server back-end.

Finally, a totally different idea suggested to me recently was using this for distributed computing. Could one create htmlwidgets that run some code in the user's browser and send results back home? (For instance, running one MCMC chain of a Stan model?)

Thoughts?

goldingn commented 6 years ago

Just wanted to add that greta can compile a large subset of R code (even if it doesn't have stochastic elements) to a tensorflow graph, which could be run in-browser with tensorflow.js (or remotely/on GPUs or on a smartphone/tablet)

I've also considered breaking that functionality out into a separate package that doesn't do statistical modelling, and/or providing other backends than tensorflow (even C++?)

noamross commented 6 years ago

Indeed, I sort of left out tensorflow (and keras) because there's already so much going on with them in this paradigm via tensorflow.js (and keras.js). But a neat idea might be something like a greta-specific widget that executes via tensorflow.js, sort of like shinystan.

goldingn commented 6 years ago

Yeah, that would be ace! If we go down that route, it might be worth checking in with JJ, in case he's planning tensorflow js integration as part of the RStudio tensorflow suite.

@michaelquinn32 might be interested in something tensorflowy like this?

michaelquinn32 commented 6 years ago

Add it to the list!

Sorry, but I don't have a sense of priority yet, and I REALLY need to read the greta internals to see where additions will be reasonable.

From the apps side, there's also all of the tensorflow serving work to think about. @javierluraschi has been doing a bunch of awesome work on that too. https://github.com/rstudio/tfdeploy

I'm not sure about evaluating the tradeoff between doing all of it in app vs treating the app as a client.

goldingn commented 6 years ago

This list might be handy: https://github.com/terrytangyuan/ctv-model-deployment

noamross commented 6 years ago

Despite the potential of tensorflow.js, I'm still pretty interested in the other pipelines I describe above, (WebAssembly and alasql), especially from the perspective of leveraging C/SQL infrastructure already developed for R so as to make it available browser-side. I suggest we discuss ideas for both here, and we might split these out into different options during the issue-consolidation phase closer to the unconf.

goldingn commented 6 years ago

Yep, good idea!

noamross commented 6 years ago

Not a lot of other conversations here, but I basically see three current projects described:

I'm still sort of interested in other out-of-the-box WebAssembly ideas.