livelink plugs into 'knitr' two ways, and which you want depends on one question: should the code also run in your document?
A chunk hook, on an ordinary r chunk. The chunk runs as usual – its
output, plots and all, appear in the rendered page – and a link is added
underneath. Use this for code you want your reader to see the result of and
also be able to open and play with.
```{r}
#| livelink: true
#| autorun: true
# Load the data
data(mtcars)
plot(mtcars$mpg, mtcars$wt)
```An engine, as ```{livelink}. The chunk is displayed but not
run: only the link is produced. Use this for code your session cannot or should
not execute – a Shiny app, something needing a package you have not installed,
or anything slow.
```{livelink}
#| engine.target: shinylive-r
library(shiny)
shinyApp(fluidPage(), function(input, output) {})
```There is deliberately no {shinylive-r} or {shinylive-py} engine. 'knitr'
cannot dispatch a chunk whose engine name contains a hyphen (its chunk syntax
forbids it), and in Quarto such a cell is handed to the Shinylive extension
rather than to 'knitr'. Name Shinylive through engine.target instead.
Value
Called for their side effect. Invisibly TRUE if registration
happened, FALSE if 'knitr' is not installed.
Details
Reach for either of these rather than expression input
(webr_repl_link({ ... })) inside a knitted document. 'knitr' evaluates
chunks through evaluate::evaluate(), which discards source references, and
comments live in those – so comments inside a { } expression are silently
dropped from the link when the document renders, and no keep.source setting
brings them back. Both the hook and the engine are handed the chunk's verbatim
source, so nothing is lost.
Both are registered automatically when livelink is loaded, provided 'knitr' is
installed. Call these yourself only if you have reset knitr::knit_hooks or
knitr::knit_engines.
Chunk options
livelinkHook only.
truefor a webR link, or name the target directly:"webr","shinylive-r","shinylive-py".engine.targetEngine only.
"webr"(default),"shinylive-r", or"shinylive-py".autorunLogical. Run the code as soon as the link opens. webR only.
panelsCharacter vector of webR panels, e.g.
c("editor", "plot").modeShinylive only. Display mode,
"editor"(default) or"app".filenamewebR only. Name for the script file in the webR virtual filesystem (default
"script.R"); must end in.Rforautorunto work.link.textText for the hyperlink. Defaults to
"Open in webR"or"Open in Shinylive".link.onlyEngine only. If
TRUE, emit the link without the source.
Setting options once
These are ordinary 'knitr' chunk options, so opts_chunk sets them for a whole
document, and a single chunk opts out with livelink: false:
echo does not gate the link
It is natural to assume the code must be visible for a link to be made. It need
not be. echo controls whether the source is shown in your page; the link is
built from the chunk's source, which 'knitr' hands over either way. So
echo: false gives a working link whose code the reader simply cannot see.
eval: false is the other half: the chunk is displayed but not run, which makes
an r chunk behave rather like the engine.
See also
vignette("links-in-documents", package = "livelink") for the whole
picture, and webr_repl_link() for why a braced expression loses comments in
a knitted document.