Version 2.0.0 of MetaR has introduced instant refresh for composable R scripts. This feature is illustrated in a GIF in the following tweet:

As you can see, it has become possible to modify an R script and very quickly see the impact of a change on the plot that the script produces (there is no need to hit run/source, the change is detected automatically, execution triggered and the display updated). Refresh occurs in less than two seconds in this case. The reason instant refresh is fast is that in most cases, the entire script does not need to be re-executed when a change is detected. Instant refresh uses the point of change to calculate the minimum set of instructions necessary to refresh the script outputs.

Instant refresh works well when coupled with another feature of MetaR 2, which enables capturing plots and showing a preview of them inside the R script:

Notice the export plot -> Output  { } block at the top of the animation.  This block identifies the set of expressions that produce a plot and give the plot a name. Once the plot is named, the multiplot statement is used to preview the plot content. You will be familiar with multiplot if you have used MetaR before. The  multiplot statement helps define multi-panel figures. In this case, we use it to show the content of one plot embedded into the R script.

The features work together because the R script is written in composable R, a version of the R language that can be extended with new kinds of expressions. Composable R is more flexible than the traditional R language since we can embed plots, buttons and other graphical elements directly in the source code of the script.

In addition to graphics, we can extend the language with expressions  that offer new semantic. We introduced the Save Session expression. Save Session gives users the ability to save an R session, and instant refresh the ability to quickly identify where a session was saved in the script. An extension was also introduced to install or load R packages. Both extensions are used by instant refresh to determine the closest Save Session sufficient to restart execution of the script, as well as the list of packages that must be loaded in the partial execution. The semantic of these new expressions therefore benefits usability (in the case of installOrLoad, users get installation or loading of a package as necessary so that the script will run well even on machines when a package was never installed), but it also benefits the implementation of instant refresh, since we do not need to perform complex pattern matching to try to guess what packages are loaded and used by a script.


For instance, consider a change made at the blue arrow in the following snapshot:


Only the expressions marked by red brackets need to be re-executed because the state of the environment before Save Session can be recovered by loading the session.

This features illustrate the first steps towards a composable R notebook where results and code are mingled and results update as the code is changed.

Instant refresh was developed by Alexander Pann. Alex joined the lab from June to August 2016 for a summer internship and did a fantastic job prototyping instant refresh. He developed two versions of it, the first one with Jupyter (released in MetaR 1.9). The first version supported MetaR analyses only, but was complex because of the client server code needed to communicate with Jupyter. In the second version, we dropped Jupyter for direct R evaluation. This sped up execution times dramatically as well as simplified the architecture and made it more robust. The second iteration enabled to support composable R scripts with instant refresh as well as partial re-execution.