Loading spice registers a spice engine with knitr (when knitr is
installed), so an R Markdown or Quarto document can hold a deck in a
chunk of its own and plot the results in the R chunks that follow:
Details
```{spice rc}
rc low-pass
vin 1 0 pulse(0 1)
r1 1 2 1k
c1 2 0 1u
.tran 0.1m 5m
.end
```
```{r}
ggplot2::autoplot(spice_plots(rc)[[1]])
```The chunk body is the netlist, title line first, exactly as
spice_run() takes it as text (.include cards are not read from
disk; see spice.includes below). The engine runs it and assigns the
spice_result to a variable in the knitting environment.
Chunk options
echo: show the deck (defaultTRUE, as for any chunk).spice.output(oroutput): what the chunk prints."tidy"(default) printsspice_tidy()of the result, as a tibble prints (its first rows), followed by any notes and parser diagnostics;"raw"prints the result itself (seespice_run(): the plots and their sizes, notes and diagnostics).results = "hide"prints nothing.outputis read only when it is one of these two strings, so Quarto's ownoutput: falsekeeps its meaning.spice.var: the name of the variable that receives the result. Defaults to the chunk label; chunks without a label (knitr'sunnamed-chunk-N) assign nothing unlessspice.varis given.spice.var = FALSEassigns nothing. A label such asrc-tranis used as is, and is then read back as`rc-tran`.spice.options: a named list passed asoptionstospice_run().spice.includes: a named list passed asincludestospice_run().
Errors
A deck that fails to parse, build or simulate stops the knitting
with the spice_error of spice_run(), whose message carries the
file:line:col of the offending card (the chunk body is the file
deck.cir). With the chunk option error = TRUE (or 0) the
message is printed in the document instead and knitting goes on.
Note that knitr::knit() defaults to error = TRUE, while R
Markdown and Quarto default to error = FALSE.
Examples
# The engine knitr calls for a ```{spice} chunk:
"spice" %in% names(knitr::knit_engines$get())
#> [1] TRUE