livelink plugs into 'knitr' two ways, and which you want depends on one question. Should the code also run in your document?
Value
Called for their side effect. The value is returned invisibly.
TRUEif registration happened.FALSEif '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 the source R kept, and
comments live in that. Comments inside a { } expression are therefore
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.
A chunk hook
Set 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.
An engine
Written as ```{livelink}. The chunk is displayed but not run, so
only the link is produced. Use this for code your session cannot or should not
execute, such as 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'
will not accept 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.
Chunk options
livelinkHook only. Use
truefor a webR link, or name the target directly with"webr","shinylive-r", or"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 webR creates in the browser (default
"script.R"). It must end in.Rforautorunto work.link.textText for the hyperlink. Defaults to
"Open in webR"or"Open in Shinylive".link.onlyEngine only. If
TRUE, show 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.
webr_repl_link() for why a braced expression loses comments in a knitted
document.