weft lets the prose of an R Markdown or Quarto document carry computed values, and wording that depends on those values. It checks that what a sentence says is still true of the data each time the document renders. For a long report or a book, it also stops the commits that would break the link between a number in the text and the analysis that produced it.
Every weft construct is markdown that knitr, Pandoc and Quarto already accept. Nothing is compiled, and the file you edit is the file that renders.
A Quarto document. The front matter turns weft on for this one document.
---
title: "Creek monitoring"
engine: knitr
knitr:
opts_knit:
weft: !expr weft::weft_setup()
---
```{r}
#| label: data
turbidity <- c(Upper = 4.2, Middle = 7.9, Lower = 12.6)
n_high <- sum(turbidity > 10)
change <- -0.084
```
We sampled `r length(turbidity)`{.word} sites.
[No site | One site | # sites]{n="n_high"} read above the trigger.
Mean turbidity [rose by # | fell by #]{.pct sign="change"} on last year,
and the highest reading was `r claim(max(turbidity), . < 50)`{.num} NTU.It renders as:
We sampled three sites. One site read above the trigger. Mean turbidity fell by 8.4% on last year, and the highest reading was 12.6 NTU.
If the data change so that no site reads above the trigger, the second sentence becomes "No site read above the trigger." If the highest reading reaches 50, the render fails and names the line, instead of printing a sentence that has stopped being true.
weft is not on CRAN. Install the development version from GitHub:
# install.packages("pak")
pak::pak("vorpalvorpal/weft")or
# install.packages("remotes")
remotes::install_github("vorpalvorpal/weft")weft uses the units package, version 1.0-1.7 or later, which is on GitHub and
not yet on CRAN (Remotes: in DESCRIPTION points to it, so
remotes::install_github() installs it; with an older units, install
r-quantities/units first). The package needs the udunits-2 library. The CRAN
binaries for macOS and Windows include it. On Linux, install your
distribution's udunits-2 development package first (for example
libudunits2-dev on Debian and Ubuntu).
Values and formatters. Inline R with an attribute block, such as
`r x`{.num}, prints a computed value through a formatter: a number, a
percentage, a spelled count, a list joined with "and". Inline R with no
attribute block prints exactly as knitr prints it. See
Values and formatters.
Wording that depends on a value. A span such as
[a site | # sites]{n="k"} chooses its words from a value: a plural form, a
direction, a condition being true. Each case you expect gets its own
wording. See
Value-dependent wording and claims.
Claims. claim(x, . > 10) returns x unchanged and checks it. When a
check fails, the render fails with a report that names the sentence, instead
of printing a sentence the data no longer support. See
Value-dependent wording and claims.
Units and the dictionary. u(35, m) makes a number with a unit, and weft
prints the unit for you. The dictionary file dict.yml holds the values a
project chose, such as a threshold or a reporting year, each with its
definition. See
Units and the dictionary
and Using weft with targets.
Python and Julia chunks. In an .Rmd, or a .qmd with engine: knitr,
a Python value reaches prose as `r py$depth`{.num} and a Julia value
as `r jl_value("depth")`{.num}. weft reads each value itself and
refuses one that R cannot hold exactly. See
Python and Julia chunks.
Values by name. In a knitr document a name such as `depth`{.num}
reads R, as `r depth` does. A document that is not knit, such as a Quarto
.qmd on the Jupyter or Julia engine, has no R: publish the value from Python
(put()), Julia (@put) or a pipeline's values file (weft_values_write()),
and a Pandoc filter fills it in. The same filter resolves conditions on names,
wording, checks and values files. See
Values by name.
weft in strings. weft() resolves the same markup in an R string, for
text that never passes through prose: plot titles and axis labels, console
and log messages, table cells. It evaluates at the call, and a name in it
reads R there. A plot or the console gets plain text, with Unicode
superscripts and no markup; a knit gets markdown.
n_high <- 2
worst <- 12.63
w <- weft("[No site | One site | # sites]{n=n_high} read above the trigger; the worst was `worst`{.num} NTU.")
as.character(w)
#> [1] "2 sites read above the trigger; the worst was 12.6 NTU."
p + ggplot2::labs(title = w) # the same text as a plot's titleTo state a number's format once and have it hold at every sink, make a self-formatting value with val_num(), val_pct(), val_sci() or val_p(). See weft in strings.
Lints. lints() reads a project's documents without running them and
lists typed numbers, construct errors and other problems. See
The static lints.
The commit gate and verify(). A git hook refuses a commit that adds a
typed number or a construct error, or that removes computed values without
saying so. Each render writes a ledger of the values, wording and tables it
printed, which changes() compares between commits, and checks the rendered
output for references Quarto could not resolve and values printed as "NA".
verify() renders the whole project and checks every claim before a release.
See
The commit gate, the ledger and verify().
Setting up a project. use_weft() writes everything a project needs, and
says what it will write first. See
Setting up a project,
and, to bring a long report across,
Adopting weft in an existing report.
If an AI coding agent edits your documents, read
Working with coding agents,
which also says how to install the skill into Claude Code without a project
(/plugin marketplace add vorpalvorpal/weft).
Start with Getting started.
Version 0.1.0 was the first release; the development version is ahead of it, and NEWS.md lists what has changed. weft is not on CRAN, and its
functions and file formats may still change. The design is in
dev/PACKAGE-SCOPING.md in the repository. Please report problems at
https://github.com/vorpalvorpal/weft/issues.