Skip to content

Instantly share code, notes, and snippets.

@baptiste
Created August 16, 2025 22:22
Show Gist options
  • Save baptiste/759a1cafc3157b38bc7979b1994cdf0e to your computer and use it in GitHub Desktop.
Save baptiste/759a1cafc3157b38bc7979b1994cdf0e to your computer and use it in GitHub Desktop.
[
{
"func": "metadata",
"value": {
"code": "x = 1 + 1",
"id": "a"
},
"label": "<r-chunk>"
},
{
"func": "metadata",
"value": {
"code": "y = 2 + 2",
"id": "b"
},
"label": "<r-chunk>"
}
]
#let code(x, id: "a") = {
[#metadata((code: x.text, id: id)) #label("r-chunk")]
}
With this code,
#code(`x = 1 + 1`, id: "a")
or another
#code(`y = 2 + 2`, id: "b")
#let output = {
if "results" in sys.inputs.keys() {
json(sys.inputs.results)
} else { // default values
(x: 3, y: 4,)
}
}
`x` is #output.x
`y` is #output.y
{"x":2,"y":4}
library(jsonlite)
library(evaluate)
library(purrr)
inputs <- jsonlite::read_json("inputs.json")
e <- new.env()
walk(inputs, \(.x) evaluate(.x[["value"]][["code"]], envir = e))
jsonlite::write_json(as.list(e), 'results.json', auto_unbox=TRUE)
@baptiste
Copy link
Author

typst query --pretty knit.typ "<r-chunk>" > inputs.json
R -f test.R
typst compile knit.typ --input results="results.json"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment