Last active
April 26, 2023 12:44
-
-
Save trestletech/9926129 to your computer and use it in GitHub Desktop.
Example Shiny App showing the number of current sessions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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