Skip to content

Instantly share code, notes, and snippets.

@trestletech
Last active April 26, 2023 12:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trestletech/9926129 to your computer and use it in GitHub Desktop.
Save trestletech/9926129 to your computer and use it in GitHub Desktop.
Example Shiny App showing the number of current sessions.
library(shiny)
# Create a reactive object here that we can share between all the sessions.
vals <- reactiveValues(count=0)
shinyServer(function(input, output, session) {
# Increment the number of sessions when one is opened.
# We use isolate() here to:
# a.) Provide a reactive context
# b.) Ensure that this expression doesn't take a reactive dependency on
# vals$count -- if it did, every time vals$count changed, this expression
# would run, leading to an infinite loop.
isolate(vals$count <- vals$count + 1)
# When a session ends, decrement the counter.
session$onSessionEnded(function(){
# We use isolate() here for the same reasons as above.
isolate(vals$count <- vals$count - 1)
})
# Reactively update the client.
output$count <- renderText({
vals$count
})
})
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Session Counter"),
# Offer 1 panel which just displays the count of sessions the app currently
# has connected.
mainPanel(
"There are currently",
verbatimTextOutput("count"),
"session(s) connected to this app."
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment