Learn R Programming

tower

Dead simple middleware for R Shiny.

Summary

tower is a simple library for adding middleware to Shiny applications. It is inspired by the tower crate for Rust. It is designed to enable package authors and Shiny developers to extend Shiny a little bit more than what is usually possible.

You can use tower to add middlewares that forward, modify, or intercept requests in Shiny applications. This can be useful for adding logging, authentication, caching, or routing to your Shiny applications.

Installation

You can install the development version of tower from GitHub with:

# install.packages("remotes")
remotes::install_github("ixpantia/tower")

Example

We may want to add a new route to our Shiny application that adds a count to a counter every time a user visits the route. We can do this with tower by adding a middleware that intercepts the request and increments the counter.

library(shiny)
library(tower)

# Counter environment
global_counter <- new.env()
global_counter$count <- 0

# Middleware to increment the counter
increment_counter <- function(req) {
  global_counter$count <- global_counter$count + 1
  response_builder() |>
    add_body(paste("Counter is now", global_counter$count)) |>
    build_response()
}

# A very empty Shiny app (not necesarry for the demo)
ui <- fluidPage()
server <- function(input, output, session) {}

shinyApp(ui, server) |>
  create_tower() |>
  add_get_route("/counter", increment_counter) |>
  build_tower()

If you run the code above and visit the route /counter in your browser, you will see the counter increment every time you visit the route.

How it works

Basically, tower adds layers to a Shiny application. A layer is a function that takes a request and returns either a response or NULL. If a layer returns a response, the response is sent to the client and the request is not forwarded to the next layer. If a layer returns NULL, the request is forwarded to the next layer.

Copy Link

Version

Install

install.packages('tower')

Version

0.2.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

October 21st, 2024

Functions in tower (0.2.0)

build_response

Build a response
build_http_cookie

Build a cookie
build_tower

Build a 'shiny' app from a tower
set_status

Set the status of a response
print.tower

Print a tower
set_header

Set a header on a response
parse_cookies

Parse cookies
set_content_type

Set the content type of a response
create_tower

Create a new tower
response_builder

Create a response builder
req_body_form

Extract form data from a request
req_query

Extract query parameters from a request
req_body_json

Extract the request body from a JSON request
req_cookies

Extract cookies from a request
add_get_route

Add a GET route
add_route

Add an HTTP layer to a tower
add_put_route

Add a PUT route
add_post_route

Add a POST route
add_patch_route

Add a PATCH route
add_body_json

Add a body to a response as JSON
add_body

Add a body to a response
add_delete_route

Add a DELETE route
add_cookie

Add a cookie to a response
add_http_layer

Add an HTTP layer to a tower
add_server_layer

Add a server layer to a tower
app_into_parts

Into parts