R bricks

A Logic Brick can be written in R as well as in Python. R bricks run in R in your browser, through webR, R compiled to WebAssembly, and wire to Python bricks in the same dagboard: a table that an R brick returns reaches a Python brick as a table, and the other way round.

Write an R brick

Make's Logic Brick opens the Logic Brick editor, whose Code panel offers Python and R in its header. Pick R, and the editor starts from an R brick, highlights R and checks the code with R's parser as you type. A saved brick keeps its language, which the editor and the dagboard's graph show on it.

An R brick is an R file that defines a function named handle, with roxygen tags above it:

#' Raises a number to a power
#'
#' Each value of x to the power of exponent.
#' @param x numeric The number
#' @param exponent numeric The power to raise it to
#' @return numeric
handle <- function(x, exponent = 2) {
  x ^ exponent
}

input_fixtures <- list(x = 3)

Save, Run, Stop and adding the brick to a dagboard work as for a Python brick (Logic Bricks).

The types

A tag's type is its first word, with what its parentheses hold. Each one is what a Python brick has for it:

A missing value is NULL, and a missing element NA. Factors go as strings. A number too large for R's integers comes as bit64's integer64, and a time to the nanosecond as a nanotime.

The library

The app's default sources hold rexamples, a library of R bricks that every dagboard can use and that you can read as examples: R's sample data sets, such as AirPassengers and mtcars, the seasonal decomposition of a series, ARIMA fits and forecasts, generalised linear models, linear mixed-effects models, robust regression, Kaplan-Meier curves and Cox models, and two-sample tests. Its bricks' names start with rexamples/, such as rexamples/ts.stl.

R and Python in one dagboard

Wire an R brick into a Python brick, or a Python brick into an R one, as you wire any two bricks: the values cross as the types above. The Samples page has dagboards in R, such as Forecast with ARIMA, Regression with R's glm and Growth by child with R's nlme, and two that mix the languages: Forecast in Python and R, where Python cleans the sales, R forecasts them and Python scores the forecast, and Survival in Python and R, where R's survival and Python's statsmodels analyse the same trial side by side.

What holds when R runs

Next: Converting code and spreadsheets