问题
Is it possible to count the number of different visitors in an online shiny R app in time?
Thank you in advance
回答1:
You could define a reactive counter inside global.R that gets increased whenever a user connects and decreased whenever a user disconnects. Here is an example.
library(shiny)
# put this line in global.R in case you want to launch the app
# with runApp (from a directory) rather than shinyApp
nvisitors = reactiveVal(0)
server = function(input, output, session){
nvisitors(isolate(nvisitors()) + 1)
onSessionEnded(function(x){
nvisitors(isolate(nvisitors()) - 1)
})
output$text = renderText({
nvisitors()
})
}
ui = shinyUI(
textOutput("text")
)
shinyApp(ui, server)
Counting the total number of visitors is more challenging tough since you need to implement persistent data storage to do that properly.
来源:https://stackoverflow.com/questions/43051537/knowing-visitors-identity-on-shiny-r-app