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)
-
The roxygen tags are the contract. R has no type hints, so the app reads the tags to
know the brick's inputs and output:
@param <name> <type> <description>for each input, and@return <type>for the result. An input whose tag names no type takes its default's, and a result with no type takes R's own: a vector of one is a single value, a longer one a list, and a matrix an array. A named list with no type gives an output per name, as a Python brick's dict does. To return it as one map, declare it as one:@return list(numeric). -
The text before the first tag is what people see when they pick the brick, as a Python
brick's docstring is, with the text of any
@title,@descriptionand@detailstags after it. -
Defaults are
handle's own, such asexponent = 2. Write an integer's as5L, since5is a double in R. -
Several outputs: return a named list, and declare it as
@return list(mean = numeric, label = character), for an output per name. -
input_fixtures, a named list, gives a sample value for each input without a default. -
Errors: call
stop()with a message that says what to fix, such asstop("window must be positive"). The dagboard shows it on the brick with its line, and the bricks that depend on it don't run. -
Logs: what
print()andcat()write, and the messages ofmessage()andwarning(), show with the brick's run. -
A Link Brick in R is marked by
brick_kind <- "link"beside itshandle. Make's Link Brick in R starts from one that reads a CSV file from the web withread.csv().
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:
-
single values:
numeric(ordouble) is afloat,integeranint,characterastr,logicalabool,Dateadatetime.date,POSIXctadatetime.datetimein UTC, andrawbytes. An R brick gets each as a vector of one; -
a list:
vector(numeric), a vector in R and a list in Python, andmatrix(numeric), an array; -
a series:
series(numeric, index = POSIXct), apd.Seriesin Python, which R gets as a vector, named when its index is strings, or else as adata.frameof its index and avaluecolumn; -
a table:
data.frame, apd.DataFrame, with a type per column where you name them, such asdata.frame(numeric, index = POSIXct). A table's index comes as columns, and adata.frame's row names go as its index; -
a timeseries:
timeseries(numeric), what the charts over time draw, a line per id (Timeseries): adata.frameoftimestamp,idandvalue, whose"index"attribute isc("timestamp", "id"); - a map:
list(numeric), a named list in R and a dict in Python.
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
- R downloads on first use. The first R brick that runs downloads R, once. A page that runs no R brick downloads none of it, so Python-only dagboards don't wait for it. After that the app keeps R in your browser, and R bricks run offline too.
-
Only the app's packages. R has base R, with stats, utils and the rest of its own
packages, and the recommended packages the app ships: MASS, Matrix, lattice, nlme and survival. A
package loads the first time a brick calls
library()on it, or names it with::, as innlme::lme. There's noinstall.packages(), so a brick that loads another package fails. - Time is UTC. An R brick sees UTC, whatever your computer's time zone, as a Python brick does.
-
Requests follow your browser's rules, as for Python: a host that a brick reads, such as
with
read.csv(url), must let pages from other sites read its answers (What holds in the browser). An R brick gets no workspace variables or secrets either. - Cancel stops R. Cancelling a run interrupts the brick, and the next one runs in the same R.
- From your workspace. R bricks run from your workspace and the default sources' libraries. An R brick from a git repo you add as a source doesn't run yet.
- Not inside an AI app. R needs the app's own tab, which the browser isolates from other sites, so R bricks don't run where an AI app shows dagboard.ai in its chat.
- Browsers. R bricks are checked in Chromium-based browsers, such as Google Chrome and Microsoft Edge, first (Browsers).