Skip to contents

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 (default TRUE, as for any chunk).

  • spice.output (or output): what the chunk prints. "tidy" (default) prints spice_tidy() of the result, as a tibble prints (its first rows), followed by any notes and parser diagnostics; "raw" prints the result itself (see spice_run(): the plots and their sizes, notes and diagnostics). results = "hide" prints nothing. output is read only when it is one of these two strings, so Quarto's own output: false keeps its meaning.

  • spice.var: the name of the variable that receives the result. Defaults to the chunk label; chunks without a label (knitr's unnamed-chunk-N) assign nothing unless spice.var is given. spice.var = FALSE assigns nothing. A label such as rc-tran is used as is, and is then read back as `rc-tran`.

  • spice.options: a named list passed as options to spice_run().

  • spice.includes: a named list passed as includes to spice_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