Starts a Shiny app – a directory or an app file – in a background R
process and returns a handle for its lifecycle. One app can back any
number of pages: pass the handle to pz_open() for each page. Closing
those pages leaves the app running.
Usage
pz_serve_shiny(app, ..., envvars = NULL, shiny_options = list(), timeout = 10)Arguments
- app
A path to a Shiny app directory or an app file (anything
shiny::runApp()accepts as a path).- ...
Reserved; must be empty.
- envvars
Named character vector of environment variables set in the app process, on top of the inherited environment.
NULLadds no environment overrides.- shiny_options
Additional options for
shiny::runApp(), e.g.list(test.mode = TRUE).appDiris reserved; useappto select the app.hostandportare managed by paparazzi:hostdefaults to"127.0.0.1", andport(defaultNULL) picks a random free port. If startup fails because the port was taken, a new port is tried.- timeout
Seconds to wait for the app to start listening; defaults to 10.
Value
A PaparazziServe handle with public fields url and port
and methods stop(), logs(), and is_running().
Details
The handle has $stop() and $logs() methods and a print() method
showing the URL, port, and status – the one place the paparazzi API
uses methods rather than pz_*() functions. $stop() interrupts,
waits, then kills, and is idempotent, so withr::defer(app$stop())
is a safe cleanup. A finalizer stops the app as a last resort.
App stdout and stderr go to a temporary log file (never an undrained
pipe), readable mid-run with $logs().
Examples
if (FALSE) { # paparazzi:::examples_run("shiny")
app <- pz_serve_shiny(pz_example("tasks-app"))
app
# Pages opened on a handle share the app, here at two screen sizes
desktop <- pz_open(app, width = 1280)
phone <- pz_open(app, width = 390, mobile = TRUE)
pz_get_text(phone, target = "#summary")
pz_close(desktop)
pz_close(phone)
# Closing the pages leaves the app running until you stop it
app$is_running()
head(app$logs())
app$stop()
}
