Skip to contents

spigot runs a plumber API inside a Shiny app, so you can build the app like a small website: pages are hand-written HTML and JavaScript, and they call R endpoints on the same origin and port.

Shiny still hosts the app (locally, on Shiny Server or on Posit Connect), gives each browser tab a session, and keeps a websocket open to it. spigot uses that to tie each API request to the tab that sent it, to push events from R to pages, and, when you want it, to feed API calls into Shiny’s reactive code.

Installation

# install.packages("remotes")
remotes::install_github("kent-orr/spigot")

Example

library(spigot)

api <- spigot_api() |>
  plumber::pr_get("/hello", function(name = "world") {
    list(message = paste0("Hello, ", name, "!"))
  })

home <- spigot_page('<!DOCTYPE html>
<html>
<head><title>hello</title></head>
<body>
  <button id="go">Say hello</button> <span id="out"></span>
  <script>
    document.getElementById("go").onclick = async () => {
      const reply = await spigot.get("hello", { name: "Ada" });
      document.getElementById("out").textContent = reply.message;
    };
  </script>
</body>
</html>')

shiny::runApp(spigot_app(pages = home, api = api))

spigot_app() adds Shiny’s scripts and spigot.js to the page. spigot.get() calls api/hello, sends the tab’s session token, and resolves with the parsed JSON reply.

Three larger examples ship with the package:

run_example("minimal")  # one page, GET and POST, errors, an async endpoint
run_example("live")     # push to every tab, and a Shiny output driven by the API
run_example("site")     # three pages with shared templates, work in a future worker