tinyshinyserver

CRAN status License: MIT Platform

tinyshinyserver serves several Shiny apps, interactive R Markdown documents, and Quarto dashboards from one R session. A lightweight proxy forwards HTTP and WebSocket traffic to the apps, each of which runs in its own R process, and a web dashboard shows what is running and lets you restart apps.

It is intended for development and internal use, and has no authentication or TLS of its own. Before making it reachable from other machines, read Network access and security.

Features

Installation

Install the released version from CRAN:

install.packages("tinyshinyserver")

Or the development version from GitHub:

# install.packages("remotes")
remotes::install_github("lab1702/tinyshinyserver")

tinyshinyserver needs R 4.1.0 or later. R Markdown apps also need Pandoc, and Quarto apps need the Quarto CLI. Neither is an R package, so install them separately.

Quick start

The package includes four example apps and a configuration file that serves them. In R, from a directory where you want to keep a copy of the examples, run:

install.packages(c("DT", "plotly", "dplyr", "flexdashboard"))  # used by the examples
library(tinyshinyserver)

file.copy(system.file("examples", package = "tinyshinyserver"), ".", recursive = TRUE)
start_tss(config = "examples/config.json")

The examples are:

App Type Mode Also needs
sales Shiny app in one file (app.R) Resident
inventory Shiny app in two files (ui.R and server.R) On-demand
reports R Markdown flexdashboard On-demand Pandoc
dashboard Quarto dashboard Resident Quarto CLI

dashboard is resident, so it starts with the server, and the example configuration needs the Quarto CLI. Without it, use the minimal configuration below, which serves only sales.

With the server running, open:

URL Page
http://localhost:3838 Landing page, with a tile for each app
http://localhost:3838/proxy/sales/ The sales app; each app is at /proxy/{name}/
http://localhost:3839 Management dashboard

start_tss() keeps the R console busy until the server stops. To stop it, click Shutdown Server on the management dashboard or press Ctrl-C in R. Either way, the server closes all connections and stops the app processes. To shut down from a script, see Management API. If an unexpected error stops the server, start_tss() also stops the apps, and then signals the error, so that a server run with Rscript exits with a failure status that a service manager can act on.

Configuration

The server reads a JSON configuration file. This minimal configuration serves only the sales example. Save it as config.json in the directory that holds the copied examples directory, and run start_tss(), which reads config.json by default:

{
  "apps": [
    {
      "name": "sales",
      "path": "./examples/sales",
      "resident": true
    }
  ],
  "starting_port": 3001,
  "proxy_port": 3838,
  "proxy_host": "127.0.0.1",
  "management_port": 3839,
  "log_dir": "./logs"
}

To change the configuration while the server runs, see Reloading the configuration. help("config-format") gives the validation rules for every setting.

App settings

Each entry in apps has these settings:

Setting Default Description
name Required Name used in the app’s URL and log file names: 1–50 ASCII letters, digits, underscores, or hyphens. Names must be unique, ignoring letter case
path Required The app’s directory; see Writing apps
resident false true starts the app with the server and restarts it after a failure; false starts it on demand. See App lifecycle
appstart_timeout 2 Seconds, counted from the app’s start, that a request waits for the app to be ready before receiving HTTP 503; fractions are allowed, up to 86400 (one day). See Startup

Server settings

Setting Default Description
starting_port Required First port to try for apps; see Processes and ports
log_dir Required Directory for log files; see Logging
proxy_port 3838 Port for the landing page and apps
proxy_host "127.0.0.1" Network interface the proxy listens on; see Network access and security
management_port 3839 Port for the management dashboard and API, which listen only on 127.0.0.1
base_path "" Public URL prefix, such as "/shiny", when a reverse proxy serves the apps under a path; see Serving under a URL prefix
title "Tiny Shiny Server" Name shown in the browser tab and top bar of the landing and management pages; up to 100 characters
restart_delay 5 Seconds to wait before restarting a failed app, up to 86400 (one day): a resident app restarts then, and an on-demand app at the next request after that. A restart requested from the dashboard or API does not wait
health_check_interval 10 Seconds between health checks, which find app processes that have exited; up to 86400 (one day)
max_request_size_mb 100 Largest request body, in megabytes, that the proxy accepts; see Limits and timeouts
max_response_size_mb 1024 Largest app response, in megabytes, that the proxy forwards; see Limits and timeouts
idle_timeout_minutes 30 Minutes without a WebSocket message, in either direction, after which a session is closed; 0 keeps idle sessions open
max_unanswered_mb 256 Most data, in megabytes, that an app may send a WebSocket session without a message from the browser in between; 0 sets no limit. See Limits and timeouts
log_level "INFO" Least severe messages written to server.log; see Log levels

Writing apps

Entry points

Point each app’s path at a directory that contains one of these entry points:

App type Entry point Requirement
Shiny, one file app.R
Shiny, two files ui.R and server.R
R Markdown A .Rmd document runtime: shiny and Pandoc
Quarto A .qmd document server: shiny and the Quarto CLI

app.R, or ui.R and server.R, take precedence over documents, and R Markdown documents over Quarto documents. After documents, the server also accepts, as shiny::runApp() does, the Shiny file names in any letter case, and server.R alone with the page in www/index.html.

When a directory holds several documents:

To make the choice explicit, keep one entry document per directory. An R Markdown or Quarto app serves only its entry document: rmarkdown, which serves both, would otherwise render, and run the code of, any other file under the directory whose path a page or its WebSocket names, such as another .Rmd or .qmd document or an R script. A request for another document’s page receives HTTP 404, and a WebSocket for any other file is closed, whether or not its path is percent-encoded. Its other files, such as images and data, are served as usual.

If an app’s directory is missing, or holds none of these entry points, the app is not started, and server.log gives the reason:

App environment

An app’s R process starts in the app’s directory and reads the .Renviron and .Rprofile there, as R does when you run the app on its own. The app’s own environment variables, such as credentials, therefore apply to it, and a profile that activates renv gives it its own project library. That holds under a server that itself runs in a renv project: the app starts without the RENV_PROJECT variable of the server’s session, which renv’s activation script would read in place of the app’s directory, unless the app’s own .Renviron sets it.

R Markdown and Quarto documents

A Quarto document is rendered before it is served, which can take many seconds. To keep starts and restarts fast, the server renders it with the Quarto CLI, the one that QUARTO_PATH names or that PATH finds in the app’s environment, only when the rendered page ({name}.html, next to the .qmd) may be out of date, and then serves it with rmarkdown::run(), as quarto::quarto_serve() does. The app’s own R process does not need the quarto R package, so a renv project library without it works. The R session that Quarto starts to render the document has the app’s library paths and environment variables; it reads no user environment file, since the app’s already apply, and runs the app’s profile, so one that activates renv sets the library paths there too.

The server records each render it makes in {log_dir}/{app_name}_render.rds. It renders the document again when:

A page that the server did not render, such as one deployed with the app or one that has changed since the server rendered it, is served while it is newer than every other file in the app directory. What rendering writes beside the page, {name}_files, {name}_data, and {name}_cache, does not count, and neither do the server’s own log files and render records when log_dir lies in the app directory. Nor do the intermediate files that a render stopped part-way leaves, such as {name}.rmarkdown, or files that the document’s own code writes into the app directory as it renders, such as a plot that it saves: the server records the files as the render left them, with the document as it was when the render began. Neither do hidden files and directories, such as .git, .Rproj.user, and .Rhistory, except a top-level .Rprofile or .Renviron, nor the packages that renv installs (renv/library, renv/staging, and renv’s other package directories); renv.lock does count. The app’s error log records whether the document was rendered, and the output of the render.

This check does not notice:

Quarto documents, and R Markdown documents that rmarkdown serves prerendered (runtime: shinyrmd, runtime: shiny_prerendered, or server: shiny), are rendered into their own directory, as {name}.html and a {name}_files directory. Two renders of one document at once overwrite each other’s files and can leave a broken page, which a server that did not make the last render serves at every start because it is newer than the document. Therefore:

Other R Markdown documents are rendered into a temporary directory for each visit, so their directories may be shared.

Adding an app

Create the app’s directory, add an entry with its name and path to the configuration file, and click Reload Config & Restart All on the management dashboard.

Running an app on its own

While developing an app, you can run it directly from R, with tinyshinyserver stopped or on a port that the server does not use. For the copied examples:

shiny::runApp("examples/sales", port = 3001)
rmarkdown::run("examples/reports/report.Rmd",
               shiny_args = list(port = 3003, host = "127.0.0.1"))
quarto::quarto_serve("examples/dashboard/dashboard.qmd",
                     port = 3004, host = "127.0.0.1")

Each of these commands runs until it is interrupted, so run one at a time.

App lifecycle

Processes and ports

The R session that runs start_tss() serves the proxy and the management dashboard. Each running app has its own background R process, which listens on 127.0.0.1 at the app’s port; the proxy forwards HTTP requests and WebSocket messages to it. If the R session that runs the server is killed, its app processes stop too.

Apps receive ports in configuration order, starting from starting_port and skipping the proxy port, the management port, and ports already in use, including a port that another program listens on at every interface. With the example configuration and no conflicts, the four apps receive ports 3001–3004. The assignments are written to server.log and are made again at each configuration reload.

The proxy forwards traffic to an app only when the app’s own process is listening on the app’s port. If another program holds that port, such as a second server instance with the same starting_port, the proxy returns HTTP 503 and logs a warning instead of sending your users’ requests to that program. This check needs the ps package to list the process’s sockets, which it cannot do on every platform.

Resident and on-demand apps

Mode Starts Stops Use when
Resident ("resident": true) With the server; again after a process failure At shutdown, restart, or configuration reload Immediate access matters
On-demand ("resident": false, the default) At the first HTTP request or WebSocket connection When no longer used, as described below Saving resources matters more than startup delay

An on-demand app stops once it has had no WebSocket connection and no HTTP request in progress for 30 seconds. The grace period lets a reloaded or newly opened page reconnect without restarting the app. A page’s WebSocket opens only once the browser has the page, so after a large response the grace period also allows for a slow browser to receive it, at 128 KB per second, for up to 10 more minutes.

An app that is still starting at that point is left to finish, for up to 10 minutes from its start, and stops within 30 seconds after it is ready if it is still unused. A slow start, such as a Quarto render, is therefore not thrown away when the visitor’s page stops waiting. On platforms where the ps package cannot list a process’s sockets, the server cannot tell that an app is still starting, and stops it.

A failed on-demand app, such as one that stops with an error at startup or crashes while in use, starts again at the first request that comes at least restart_delay seconds after the failure. An earlier request receives HTTP 503 with a Retry-After header for the time that is left, and a browser receives the page that reloads itself, so a failing app is not started again by every reload. A restart from the management dashboard ends the wait. An app that ends itself without an error, as one that calls shiny::stopApp() does, has not failed, and an on-demand one starts again at the next request.

Startup

A request for an app that is starting waits for the app to be ready, for up to appstart_timeout seconds (default: 2) counted from the app’s start. After that, the proxy answers HTTP 503 with a Retry-After header. A browser opening the app’s page receives a page that reloads itself until the app is ready, for up to two minutes; scripts and other clients receive JSON. Increase appstart_timeout for apps that start slowly.

If an app still does not answer after 30 seconds, or after appstart_timeout if that is longer, the next request receives HTTP 502 (“App startup timed out”) once, and later requests receive HTTP 503 again. The server keeps checking an app whose process is still running for up to 10 minutes from its start, and logs it as ready once it answers.

Failures and restarts

Every health_check_interval seconds, the server looks for app processes that have exited, and restarts a resident app after restart_delay seconds. An app process that exits with an error, or is killed, has failed, and server.log says that the app died; one that ends without an error, as an app that calls shiny::stopApp() does, is logged as having exited. These checks do not test whether a running app responds or produces correct results.

Monitoring and management

Page Default URL Shows
Landing page http://localhost:3838 A tile for each app, with its status and connection count
Management dashboard http://localhost:3839 Server uptime and memory use, R version, and operating system; each app’s mode, status, process ID, port, path, memory use, and uptime; and each WebSocket connection’s client IP address, user agent, connection time, and last activity

Both pages refresh their status every 5 seconds. They follow the system’s light or dark theme by default; the theme button in the top bar switches between the two, and the browser remembers that choice until you switch back to match the system.

App status

Status Meaning
running The app’s process is running
dormant An on-demand app that is not running, including one whose process has exited; opening it starts it, after restart_delay if its process failed
unavailable An on-demand app whose directory cannot be served; server.log gives the reason
stopped A resident app that is not running, such as one waiting to be restarted
crashed A resident app whose process has exited and has not yet been cleaned up by a health check

The landing page links to running and dormant apps. Tiles for unavailable, stopped, and crashed apps are disabled, and all tiles are disabled while the server is unreachable.

Client IP addresses are taken from X-Forwarded-For or X-Real-IP only when the request comes from a reverse proxy on the same machine, and only when the value is an IP address. The last X-Forwarded-For entry is used, so the reverse proxy must add the client’s address to that header rather than pass on the one the client sent; Caddy does so by default, and nginx needs proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;. Behind a reverse proxy on another machine, the dashboard shows that proxy’s address.

Memory figures are the resident memory (RSS) of the server’s own R process and of each app’s R process. Processes that an app starts itself, such as Pandoc or a database driver’s helper, are not included. An app’s uptime starts from zero whenever its process starts again. Memory shows as N/A on platforms where the ps package cannot read it.

Restarting an app

Running, stopped, and crashed apps can be restarted from the management dashboard. A restart disconnects the app’s users once their app’s process has stopped; if it cannot be stopped, the restart fails and they stay connected. A resident app starts again at once, without waiting for restart_delay; an on-demand app stops and starts again when it is next opened. Dormant apps start when opened, and an unavailable app becomes dormant once its directory can be served. A dormant app whose process failed shows how long it waits before it can start again, and restarting it ends that wait.

Reloading the configuration

Reload Config & Restart All on the management dashboard reads the configuration file again, stops every app, and disconnects their users. It then assigns app ports under the new configuration and starts the resident apps; on-demand apps start when next opened. Use it to add, remove, or change apps, or to apply a changed log_level, without restarting the server.

The new file is checked first. Nothing is stopped if the file is invalid, if the server log cannot be written in its log_dir, if other programs hold the ports that the apps would need, or if it changes proxy_host, proxy_port, or management_port; those three settings take effect only when the server restarts. If an app cannot be stopped, the previous configuration stays in effect, the users of that app stay connected, as when a restart cannot stop an app, and the stopped resident apps start again. The dashboard reports the outcome once the reload finishes.

Proxy endpoints

These read-only endpoints are served on the proxy port. Like the landing page, they answer the other methods that the proxy forwards with HTTP 405, and methods that it does not forward at all with HTTP 501.

Endpoint Method Description
/health GET Returns {"status": "healthy"} while the proxy is responding; does not check the apps
/api/apps GET Name, status, mode, and connection count of each app, used by the landing page

Management API

These endpoints are served on the management port. POST requests must carry the header X-TinyShinyServer-Request: management, which browsers send from another website’s page only where CORS allows it; the dashboard sends it automatically. The dashboard and the GET endpoints also answer HEAD, and an endpoint asked with a method it does not serve answers HTTP 405. The header is checked first, so a POST without it receives HTTP 403 from every endpoint, and a method that the server does not recognize at all, such as PROPFIND, receives HTTP 501.

Endpoint Method Description
/api/status GET Server overview: app and connection counts, uptime_seconds, server_memory_bytes, apps_memory_bytes (the total of running apps), and the outcome of the latest configuration reload
/api/apps GET Detailed status of each app, including pid, memory_bytes, and uptime_seconds for running apps, and restart_in_seconds for a dormant app whose process failed
/api/connections GET Details of each active WebSocket connection
/api/apps/{name}/restart POST Restart the named app
/api/reload POST Reload the configuration file and restart all apps
/api/shutdown POST Shut down the server

The POST endpoints answer JSON with success and message fields. A request refused before it reaches the action, such as one without the required header or with an invalid app name, receives JSON with an error field instead. A POST without the required header receives HTTP 403, a restart of an app that is not configured receives HTTP 404, and a reload of an invalid configuration receives HTTP 400 with the reason. An accepted reload runs after its response is sent; its outcome appears in /api/status as last_reload once it finishes.

For example:

# Get the server status
curl http://localhost:3839/api/status

# Restart the sales app
curl -X POST -H "X-TinyShinyServer-Request: management" http://localhost:3839/api/apps/sales/restart

# Reload the configuration and restart all apps
curl -X POST -H "X-TinyShinyServer-Request: management" http://localhost:3839/api/reload

# Shut down the server
curl -X POST -H "X-TinyShinyServer-Request: management" http://localhost:3839/api/shutdown

Network access and security

proxy_host sets the network interface that the proxy listens on. The management server and the apps always listen on 127.0.0.1 only.

proxy_host Reachable from
"127.0.0.1" (default) or "localhost" This machine, over IPv4
"::1" This machine, over IPv6
"0.0.0.0" All IPv4 interfaces
"::" All IPv6 interfaces, and on most systems all IPv4 interfaces too

Security model

The server has no authentication or TLS of its own. For access from other machines, put an authenticated HTTPS reverse proxy in front of it, and use firewall rules so that only the reverse proxy can connect to the proxy port. Also keep in mind:

Reverse proxy requirements

The proxy uses the Host header to block DNS rebinding, and accepts an app’s WebSocket connection only when the browser’s Origin matches the public host. A reverse proxy must therefore send these headers:

Caddy example

This configuration serves the apps and the management dashboard on two host names, each with a password. Keep proxy_host set to "127.0.0.1", and replace the domains, usernames, and password hashes. Generate each hash with caddy hash-password; Caddy’s basic_auth directive requires hashed passwords. The browser sends the password with every request, in the Authorization header, and header_up -Authorization keeps it from being passed on to every app.

myapp.example.com {
    reverse_proxy 127.0.0.1:3838 {
        header_up Host {upstream_hostport}
        header_up -Authorization
    }
    basic_auth {
        username REPLACE_WITH_PASSWORD_HASH
    }
}

manage.myapp.example.com {
    reverse_proxy 127.0.0.1:3839 {
        header_up Host {upstream_hostport}
    }
    basic_auth {
        admin REPLACE_WITH_ADMIN_PASSWORD_HASH
    }
}

Serving under a URL prefix

To serve the apps under a path on an existing site, such as https://example.com/shiny/, set base_path to that path:

"base_path": "/shiny"

The landing page is then at /shiny/, and each app at /shiny/proxy/{name}/. Links, redirects, and cookie paths include the prefix. The proxy accepts requests with or without the prefix, so the reverse proxy may either pass the path through unchanged or strip the prefix. Every app therefore has two addresses at the proxy, /shiny/proxy/{name}/ and /proxy/{name}/: where the reverse proxy limits access by path, its rules must cover both, and it should forward only paths under the prefix, since behind one that strips the prefix, /shiny/shiny/proxy/{name}/ reaches the same app. In Caddy:

example.com {
    redir /shiny /shiny/ 308

    # Pass the path through; use handle_path instead to strip the prefix
    handle /shiny/* {
        reverse_proxy 127.0.0.1:3838 {
            header_up Host {upstream_hostport}
        }
    }
}

In nginx, location /shiny/ { proxy_pass http://127.0.0.1:3838; } passes the path through, and proxy_pass http://127.0.0.1:3838/; strips it. The reverse proxy requirements still apply.

The first segment of base_path may not be proxy, api, templates, or health, which the proxy already uses. Once base_path is set, open the apps through URLs that include it: app cookies are scoped to the prefixed path, so an app opened at http://localhost:3838/proxy/{name}/ does not receive them.

The management dashboard uses relative URLs and needs no setting. A reverse proxy may serve it under a prefix that it strips, opened with the trailing slash, but on its own host name, as described in Reverse proxy requirements.

Limits and timeouts

Requests:

Responses:

WebSocket sessions:

Logging

Log files

The server writes these files to log_dir:

File Contents
server.log The server’s own log
{name}_output.log The app’s standard output
{name}_error.log The app’s standard error, including R errors and warnings
{name}_output.prev.log, {name}_error.prev.log The same logs from the app’s previous run
{name}_render.rds For a Quarto app, the record of the server’s renders of its document; see R Markdown and Quarto documents

App output goes straight to these files and is not held in memory. When an app starts, its logs from the previous run are kept as the .prev.log files, so the output of a crash survives the automatic restart.

The server does not rotate or trim its logs. server.log grows for as long as the server runs, and the next start appends to it; the server opens it for each line it writes, so a tool such as logrotate can rotate it while the server runs. An app’s logs grow until the app restarts.

To follow the server log from a terminal:

tail -f logs/server.log
Get-Content logs/server.log -Wait

Log levels

log_level chooses the least severe level written to server.log (default: "INFO"). A configuration reload applies a changed level.

Level Records
"DEBUG" Each request’s routing, port probing at configuration reloads, connection bookkeeping, the full address of each app WebSocket connection, and the first 100 bytes of every WebSocket message
"INFO" Normal operations, such as apps starting and stopping and connections opening and closing
"WARN" Warning conditions, such as a session closed for a limit, or a refused WebSocket handshake
"ERROR" Errors that need attention

A refused WebSocket handshake, such as one from another website’s page, or one for an app that cannot start or whose process is not listening on its port, is logged at WARN at most once every 10 seconds for each reason, and for each app when the reason lies with the app, with the number of others since then, which are logged at DEBUG. A page that opens handshakes in a loop therefore does not fill server.log.

WebSocket messages can include what users type, and WebSocket addresses can carry tokens in their query strings, so use "DEBUG" only while diagnosing a problem.

Troubleshooting

Apps don’t start

WebSocket connections fail

The management dashboard is not reachable

Development

Clone the repository:

git clone https://github.com/lab1702/tinyshinyserver.git
cd tinyshinyserver

Then, in R from the repository root:

devtools::install_deps(dependencies = TRUE)
devtools::load_all()  # load the package for interactive use
devtools::test()      # run the tests
devtools::check()     # run R CMD check

The code is in R/, the example apps are in inst/examples/, and the web page templates and styles are in inst/templates/. Help pages are generated from the roxygen comments in R/: edit those, then run devtools::document() to update man/.

The browser tests in tests/browser/ check the management dashboard, the landing page, and the page shown while an app starts in a headless Chromium. They need Node.js, with Playwright on NODE_PATH, and are not part of R CMD check. Run them from the repository root; management-csrf.cjs also needs Rscript on the PATH. Set TSS_TEST_BROWSER to use a specific browser executable.

node tests/browser/management-page.cjs
node tests/browser/management-csrf.cjs
node tests/browser/public-pages.cjs

To contribute, open an issue or a pull request on GitHub. Add tests for your change, and run devtools::check() before submitting.

Getting help

mirror server hosted at Truenetwork, Russian Federation.