Skip to contents

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.

Usage

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)

Arguments

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: .Rmd (case insensitive), .html, .htm, .js, .css, .sass, .scss.

...

Arguments passed on to servr::httw

dir

The root directory to serve.

watch

a directory under which httw() is to watch for changes; if it is a relative path, it is relative to the dir argument

pattern

a regular expression passed to list.files() to determine the files to watch

all_files

whether to watch all files including the hidden files

handler

a function to be called every time any files are changed or added under the directory; its argument is a character vector of the filenames of the files modified or added

render_quietly

If TRUE (default), the output from rmarkdown::render() will not be shown. Set to FALSE for debugging. You can set the default value with a global option:

options(js4shiny.live_preview.quiet = FALSE)

external

Should the live preview be opened in an external browser? The default is FALSE and the preivew is opened in the RStudio viewer pane (if launched inside RStudio).

which

A integer vector of the server IDs; by default, IDs of all existing servers in the current R session obtained from daemon_list(), i.e., all daemon servers will be stopped by default.

Value

Invisibly returns the servr::httw() object, so that you can manually stop the server with the $stop_server() method.

Functions

  • live_preview_stop: Stop the live preview background daemons. See servr::daemon_list() for more information.

RStudio Addins

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.

Examples

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()
}