Skip to content

Commit bd97d1d

Browse files
committed
Vignette "Equations in R Markdown and Quarto" added
1 parent 1c7c9ce commit bd97d1d

File tree

5 files changed

+97
-3
lines changed

5 files changed

+97
-3
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# equatiomatic 0.4.2
22

3+
* New vignette "Equations in R Markdown and Quarto" that explain how to use the
4+
three functions `equation()`, `eq_()` and `eq__()` to ease inclusion of
5+
equations in R Markdown and Quarto documents.
6+
37
* `extract_eq()` can now extract equations from a list of models. There is an
48
example of an application at the end of the "Introduction to {equatiomatic}"
59
vignette.

R/shiny.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @param outputId The identifier of the output from the server. Should be
99
#' passed as a string.
1010
#' @param env The environment
11-
#' @param quoted Is the expresion quoted?
11+
#' @param quoted Is the expression quoted?
1212
#' @param outputArgs list of output arguments
1313
#'
1414
#' @return Render the equation in a suitable way for Shiny for [renderEq()] in

inst/WORDLIST

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ docx
4848
edu
4949
eq
5050
equatiomatic’
51-
expresion
5251
frac
5352
gelman
5453
geoms
@@ -86,5 +85,6 @@ texPreview
8685
tibble
8786
tidymodels
8887
ver
88+
VSCode
8989
www
9090
yonicd

man/renderEq.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/markdown-quarto.Rmd

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)