live_preview | R Documentation |
Opens a live preview of the files in a directory. The live preview server
automatically renders R Markdown files when they are saved, and the preview
is refreshed whenever R Markdown files or supporting files, such as .js
,
.css
, .htm
, .html
, .sass
, or .scss
files, are updated. This
functionality requires the servr package.
live_preview(
path = getwd(),
update_pattern = "[.](js|css|[Rr]?[Mm][Dd]|html?|s[ca]ss)$",
...,
render_quietly = getOption("js4shiny.live_preview.quiet", TRUE),
external = FALSE
)
live_preview_stop(which = NULL)
path |
The path for the directory or file to preview. If the path given is an R Markdown document or HTML document, the HTML version of that file will be opened directly, otherwise the directory containing the file will be served. |
update_pattern |
Update the live preview when files matching this
pattern are updated. By default, updating files with the following
extensions will update the preview: |
... |
Arguments passed on to
|
render_quietly |
If
|
external |
Should the live preview be opened in an external browser?
The default is |
which |
A integer vector of the server IDs; by default, IDs of all
existing servers in the current R session obtained from
|
Invisibly returns the servr::httw()
object, so that you can
manually stop the server with the $stop_server()
method.
live_preview_stop
: Stop the live preview background daemons. See
servr::daemon_list()
for more information.
There are three Live Preview addins provided by js4shiny. Live Preview and Live Preview (External) open a live preview of the directory of the currently open document, if possible at the current HTML document corresponding to the open document. The external preview addin automatically opens the preview in your web browser, otherwise the preview is opened in the RStudio Viewer pane.
To stop the live server, you can call servr::daemon_stop()
or
live_preview_stop()
, which will stop all bakground servr daemons,
or you can use the Live Preview Stop addin.
if (interactive()) {
tmp_dir <- tempfile("live-preview")
dir.create(tmp_dir)
tmp_rmd <- file.path(tmp_dir, "js4shiny-plain.Rmd")
# Create a new js4shiny plain HTML document. If interactive
# and in RStudio, this file will open and you can use the
# addins to launch the live preview
js4shiny_rmd("js", full_template = TRUE, path = tmp_rmd)
srvr <- live_preview(tmp_rmd)
# Stop all background servers with either of the following
# live_preview_stop()
# servr::daemon_stop()
#
# Or if you've saved the return value from live_preview()
# srvr$stop_server()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.