R Knitr Integration

Living Papers Team

This article demonstrates compile-time evaluation of R code, with results stitched back into the Living Papers article. JSON-serialized values can also be bound to variables in the reactive runtime.

R version: R version 4.1.3 (2022-03-10)

Plots

Let’s make some plots.

Plot generated in R
Histogram of random variates.
Plot generated in R
Density plot of Old Faithful

Linear Model

Let’s fit a model: lm(hwy ~ displ, data = mpg)

Plot generated in RPlot generated in R
(Left) Displacement vs. highway efficiency. (Right) Residual plot.

Model summary:


Call:
lm(formula = hwy ~ displ, data = mpg)

Residuals:
    Min      1Q  Median      3Q     Max 
-7.1039 -2.1646 -0.2242  2.0589 15.0105 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  35.6977     0.7204   49.55   <2e-16 ***
displ        -3.5306     0.1945  -18.15   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.836 on 232 degrees of freedom
Multiple R-squared:  0.5868,    Adjusted R-squared:  0.585 
F-statistic: 329.5 on 1 and 232 DF,  p-value: < 2.2e-16

The fitted line is .

Et Cetera

The square root of 5 is 2.236068.