This article is for anyone who writes reports in R Markdown or Quarto
and wants the numbers in the prose to come from the analysis. After it
you will be able to turn weft on in a document, print computed values
with formatters, choose wording from a value, check a value with
claim(), and read the report weft gives when a check
fails.
What weft adds
weft adds two things to the markdown you already write.
A value is inline R code. With no attribute block,
`r x` prints exactly as knitr prints it. With an attribute
block, `r x`{.num} prints through one of weft’s
formatters.
A span is a bracket of prose with an attribute block
that names a weft key: [a site | # sites]{n="k"}. The key’s
R expression chooses which part of the bracket prints.
Both are ordinary Pandoc syntax. A value with an attribute block or a span with a weft key is a construct. The rest of your document is left alone.
Turn weft on
weft does nothing until a document calls
weft::weft_setup(). The smallest way is the document’s own
front matter:
---
title: "Creek monitoring"
output: html_document
knitr:
opts_knit:
weft: !expr weft::weft_setup()
---A Quarto document needs engine: knitr in its front
matter as well, so that knitr runs it. library(weft) in a
chunk does not turn weft on: it only attaches the package. The other
ways to turn weft on, and the installers that write them for you, are in
vignette("setup", package = "weft").
Once weft is on, inline R in the document can call
claim(), plural(), u(),
glue_vec(), dict_glossary() and the
fmt_*() functions without weft::. Call other
weft functions, such as of_n() or weft(), as
weft::of_n(), or attach the package with
library(weft) in a chunk.
A first document
This R Markdown document summarises three turbidity readings.
---
title: "Creek monitoring"
output: html_document
knitr:
opts_knit:
weft: !expr weft::weft_setup()
---
```{r data}
sites <- c("Upper", "Middle", "Lower")
turbidity <- c(Upper = 4.2, Middle = 7.9, Lower = 12.6)
n_high <- sum(turbidity > 10)
change <- -0.084
```
We sampled `r length(sites)`{.word} sites: `r sites`{.and}.
Mean turbidity was `r mean(turbidity)`{.num} NTU, and
[no site | one site | # sites]{n="n_high"} read above the trigger.
Turbidity [rose by # | fell by #]{.pct sign="change"} since last year.
The highest reading was `r claim(max(turbidity), . < 50)`{.num} NTU,
at `r names(which.max(turbidity))`.The paragraph renders as:
We sampled three sites: Upper, Middle and Lower. Mean turbidity was 8.2 NTU,
and one site read above the trigger. Turbidity fell by 8.4% since last year.
The highest reading was 12.6 NTU, at Lower.
Each construct did one job:
-
{.word}spelled the count, and{.and}joined the names. -
{.num}rounded the mean to the project’s default of one decimal place. - The
nspan chose “one site” becausen_highis 1. With no reading above 10 it prints “no site”, and with two it prints “2 sites”. - The
signspan chose “fell by” becausechangeis negative, and printed its size as a percentage in place of#. -
claim()checked that the highest reading is below 50 and returned it unchanged for{.num}to print. - The last value has no attribute block, so it printed as knitr prints it.
When the reading at Lower is 9.6 instead, the same source renders as:
We sampled three sites: Upper, Middle and Lower. Mean turbidity was 7.2 NTU,
and no site read above the trigger. Turbidity fell by 8.4% since last year.
The highest reading was 9.6 NTU, at Lower.
The words changed with the data. Nobody edited the sentence.
When a check fails
Suppose a storm pushes the reading at Lower to 61.0. The claim that the highest reading is below 50 is now false, and the render stops with this message:
weft: 1 claim failed
storm.Rmd:20 `r claim(max(turbidity), . < 50)`
. < 50 was FALSE (value 61)
Read each sentence. If FALSE is right, rewrite the sentence.
Do not loosen a claim to clear this.
A failed check does not stop the knit at once. weft collects every failure in the document, then fails the render once, with each failure’s file and line. Read each sentence the report names and decide which is wrong: the sentence, the check or the data.
A value that cannot be printed is collected the same way. An
NA given to {.num} is a “no value” failure,
and an error in a construct’s expression is reported with its line.
Draft mode
While you write, you may want to see the whole document even though a
check fails. Set the environment variable WEFT_DRAFT=1, or
put draft: true in the project’s _weft.yml. In
draft mode the render finishes, the failures are listed in the knit log,
and each failed construct carries a visible marker:
The highest reading was 61<<weft: failed claim, line 20>> NTU, at Lower.
Every marker starts with <<weft:, so you can
search for it. Draft mode covers the render only. verify()
refuses to run in draft mode, and the commit gate ignores it.
If weft is not on
A document whose constructs are never resolved does not print
plausible text. Without weft_setup(), the attribute blocks
print as text, for example “We sampled 3{.word} sites”, and a
claim() call stops the knit because claim is
not found. In a set-up project the weft-check filter also
fails the render on any leftover construct.
weft from R code
Each formatter class has a function of the same name, for use in chunks, plots and tables:
library(weft)
fmt_num(8.2333)
#> <weft_text>
#> [1] "8.2"
fmt_word(3)
#> <weft_text>
#> [1] "three"
fmt_and(c("Upper", "Middle", "Lower"))
#> <weft_text>
#> [1] "Upper, Middle and Lower"
fmt_pct(-0.084)
#> <weft_text>
#> [1] "−8.4%"plural() is the function form of an n
span:
weft() resolves weft markup in a string, for text that
no chunk option reaches, such as a table caption or a plot title. It
returns an object that prints as markdown in a knit and gives plain text
(as.character()) to anything else:
n_sites <- 3
weft("Turbidity at `r n_sites`{.word} sites.")Turbidity at three sites.
Outside a knit, a failed check stops at once with an error:
claim(61, . < 50)
#> Error:
#> ! weft: 1 failed claim
#> check `. < 50` failed (value: 61)Where next
-
vignette("values", package = "weft"): every formatter class and its settings. -
vignette("wording", package = "weft"): spans, claims, and how to see every wording a document can produce. -
vignette("units", package = "weft"): quantities with units, and the project dictionary. -
vignette("targets", package = "weft"): the dictionary in a targets pipeline. -
vignette("languages", package = "weft"): Python and Julia values in prose. -
vignette("named-values", package = "weft"): values by name, for documents whose prose cannot call R. -
vignette("strings", package = "weft"):weft()in plot titles, console messages and tables. -
vignette("lints", package = "weft"): the static checks. -
vignette("gate", package = "weft"): the commit gate, the ledger,changes()andverify(). -
vignette("setup", package = "weft"): setting up a project. -
vignette("adopting", package = "weft"): bringing an existing long report across, step by step, and choosing how hard each check fails. -
vignette("agents", package = "weft"): what an AI coding agent needs to know.