Web applications can have rich functionality nowadays. For example a website of an E-commerce shop has a page about the products they are selling, a page about their conditions, a shopping cart page and an order page. Furthermore it can handle different HTTP requests from the user. A GET request is used to retrieve a page (e.g. products) from the server whereas a POST request is used to send information to the server (e.g. make order).
Problem
Shiny apps are, by default, a bit limited when looking at it from this perspective. It can handle only GET requests by default, unless you are a technical expert in this field, see this stack overflow post. Furthermore shiny apps can have just one entry-point “/”. So you can’t have another entry-point “/page2”. Thus, the e-commerce shop is not possible out of the box in R shiny.
Solution
There are multiple solutions to support multiple pages. The one that I am using since a while is the package brochure developed by Colin Fay. It is still in development, so you might encounter some issues but I haven’t found any major bugs yet. A brochure app consists of a series of pages that are defined by an endpoint/path, a UI and a server function. Thus each page has its own shiny session, its own UI, and its own server! This is important to keep in mind. A separate session for each page has some advantages but also some disadvantages (e.g. how to pass user data between pages?). A very simple brochureApp looks like this:
library(shiny)
library(brochure)
brochureApp(
# First page
page(
href = "/",
ui = fluidPage(
h1("My first page"),
plotOutput("plot")
),
server = function(input, output, session){
output$plot <- renderPlot({
plot(iris)
})
}
),
# Second page, no server-side function
page(
href = "/page2",
ui = fluidPage(
h1("My second page")
)
)
)
Donation app
Coming back to the E-commerce shop example, I have developed an app where one can sponsor me for my open source work on R packages. The app has an integration with Stripe to make a donation and a thank you and error page. When calling Stripe you have to give the two endpoints for these pages and by using brochure, I am able to setup these endpoints. See the app on shinyapps.io and of course I would appreciate it, if you use the app!:-)