A webR chunk is created by marking the chunk {webr-r}. The code in the chunk can be edited and executed by a user. The code can be executed line by line (Cmd-Enter on macOS) or the whole chunk at once (Shift-Enter).
Regular chunks
‘Normal’ R chunks (marked with {r}) still behave as usual.
fit2 <-lm(mpg ~ am, data = mtcars)summary(fit2)
Call:
lm(formula = mpg ~ am, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-9.3923 -3.0923 -0.2974 3.2439 9.5077
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 17.147 1.125 15.247 1.13e-15 ***
am 7.245 1.764 4.106 0.000285 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 4.902 on 30 degrees of freedom
Multiple R-squared: 0.3598, Adjusted R-squared: 0.3385
F-statistic: 16.86 on 1 and 30 DF, p-value: 0.000285
Plotting
Plotting works as well. Objects can be reused between chunks - make sure that the chunk above is executed before trying to plot the fit object here!
Using packages
In order to use packages with webR, they must be compiled for WebAssembly. Many (but not all) CRAN packages are available (see https://repo.r-wasm.org/). Here, we first install ggplot2:
We can also specify the packages we want to use under the webr section in the YAML header. They will then be installed automatically when the document is opened.