|
| 1 | +--- |
| 2 | +title: "Equations in R Markdown and Quarto" |
| 3 | +output: rmarkdown::html_vignette |
| 4 | +vignette: > |
| 5 | + %\VignetteIndexEntry{Equations in R Markdown and Quarto} |
| 6 | + %\VignetteEngine{knitr::rmarkdown} |
| 7 | + %\VignetteEncoding{UTF-8} |
| 8 | +--- |
| 9 | + |
| 10 | +```{r, include=FALSE} |
| 11 | +library(equatiomatic) |
| 12 | +knitr::opts_chunk$set(collapse = TRUE, comment = "#>") |
| 13 | +``` |
| 14 | + |
| 15 | +This vignette demonstrates how to include equations in R Markdown and Quarto documents using the {equatiomatic} package and the new functions `equation()`, `eq_()` and `eq__()` in version \>= 0.4.0. |
| 16 | + |
| 17 | +## Display equations from an R chunk |
| 18 | + |
| 19 | +The most natural way to include equations in R Markdown or Quarto documents is to just print the equation generated by `extract_eq()` in an R chunk. The `knit_print()` method of {knitr} will then be used to generate a display equation (a LaTeX equation surrounded by double dollars `$$...$$`). Now, the `equation()` function can now be used in place of `extract_eq()`. It can use `label` and `units` attributes set to variables in the dataset used in the model. This is a convenient way to use friendlier variable names (and possibly units) in the equation. |
| 20 | + |
| 21 | +Here is an example using the (overused) `iris` dataset. We add `label` and `units` attributes to our variables inside the data.frame: |
| 22 | + |
| 23 | +```{r iris} |
| 24 | +data(iris) |
| 25 | +# Attach `label` and `units` attributes to the variables |
| 26 | +# Note, this is done manually, but there are functions in various packages to |
| 27 | +# ease the process (see packages {Hmisc}, {labelled}, {LabelR}, {labelVector}) |
| 28 | +attr(iris$Sepal.Length, "label") <- "Sepal length" |
| 29 | +attr(iris$Sepal.Length, "units") <- "cm" |
| 30 | +attr(iris$Sepal.Width, "label") <- "Sepal width" |
| 31 | +attr(iris$Sepal.Width, "units") <- "cm" |
| 32 | +attr(iris$Petal.Length, "label") <- "Petal length" |
| 33 | +attr(iris$Petal.Length, "units") <- "cm" |
| 34 | +attr(iris$Petal.Width, "label") <- "Petal width" |
| 35 | +attr(iris$Petal.Width, "units") <- "cm" |
| 36 | +
|
| 37 | +str(iris) |
| 38 | +``` |
| 39 | + |
| 40 | +Now, let's fit a simple linear model to those "label-augmented" `iris` data: |
| 41 | + |
| 42 | +```{r} |
| 43 | +lm1 <- lm(Sepal.Length ~ Sepal.Width, data = iris) |
| 44 | +lm1 |
| 45 | +``` |
| 46 | + |
| 47 | +If we use `extract_eq()`, we got the equation using variable names (unless we specify the `swap_var_names=` argument, of course): |
| 48 | + |
| 49 | +```{r extracteq} |
| 50 | +extract_eq(lm1) |
| 51 | +``` |
| 52 | + |
| 53 | +Now, using `equation()` instead, we use the labels and units attributes of the variables in the dataset: |
| 54 | + |
| 55 | +```{r equation-iris} |
| 56 | +equation(lm1) |
| 57 | +``` |
| 58 | + |
| 59 | +In case the model does not retain the original data with their labels, one can use the `origdata=` argument to pass the original data set to the `equation()` function. Also the `auto.labs=` argument can be set to `FALSE` to avoid using the labels and units attributes (it is `TRUE` by default). |
| 60 | + |
| 61 | +## Inline equations in R Markdown or Quarto documents |
| 62 | + |
| 63 | +Now, if you want an inline equation (an equation inside the text), you can easily use an inline R chunk calling the `eq_()` function like this: `` `r eq_(lm1)` ``. This produces `r eq_(lm1)`, thus right inside the text. Note that `eq_()` also handles `label` and `units` attributes, the same way `equation()` does. Moreover, if you issue `eq_(lm1)` in the R console or terminal (at the R prompt), you got a preview of your equation in a temporary HTML document, or in the Viewer pane if you work in RStudio. This is convenient to check the appearance of your equation without having to render the whole document. |
| 64 | + |
| 65 | +## Display equations with labels |
| 66 | + |
| 67 | +The `extract_eq()` function has a `label=` argument that allows you to set a label for the equation, but in LaTeX documents only (thus for PDF but not for HTML final documents). It does not ease cross-referencing of the equation because the label is not known from within Markdown and your Markdown editor (RStudio, VSCode, Positron...). The `eq__()` function allows to insert an equation generated with {equatiomatic} inside a `$$...$$` construct in a Markdown text. The construct can potentially have a label, like `$$...$${#eq-model1}` that can be easily cross-referenced. You just have to place an R chunk with your equation inside the `$$...$$` construct, very similarly to what you did with the inline R chunk. Thus, you would have something like this in your document: |
| 68 | + |
| 69 | +``` |
| 70 | +$$ |
| 71 | +`r eq__(lm1)` |
| 72 | +$${#eq-model1} |
| 73 | +``` |
| 74 | + |
| 75 | +which translates into: |
| 76 | + |
| 77 | +$$ |
| 78 | +`r eq__(lm1)` |
| 79 | +$${#eq-model1} |
| 80 | +
|
| 81 | +In documents that support cross-referencing (unfortunately not this vignette document), the equation is numbered at the right and you can reference it in the text with `@eq-model1`. The `eq__()` function is also aware of `label` and `units` attributes and it also allows to preview the equation when its code is issued at the R prompt. |
| 82 | +
|
| 83 | +# Customizing equations |
| 84 | +
|
| 85 | +All three functions `equation()`, `eq_()` and `eq__()` also support all the arguments of `extract_eq()` (for colors, parameters change, line wrapping, etc.). Here is an example: |
| 86 | +
|
| 87 | +```{r} |
| 88 | +equation(lm1, var_colors = c(Sepal.Width = "red"), |
| 89 | + ital_vars = TRUE, use_coefs = TRUE, coef_digits = 3) |
| 90 | +``` |
0 commit comments