Hi, RStudio Server Pro (now Workbench) it is not really made to deploy Shiny applications that non-developers consume. The RStudio Connect product is where you want to deploy your Shiny apps. This article may help clarify: Dev/Test/Prod with RStudio Team - RStudio :: Solutions. Maybe a conversation with your Customer Success representative at RStudio may help clarify any questions about this.
Here is a crude way in which you could track sessions
library(shiny)
ui <- fluidPage(
tableOutput("myOutput")
)
#Track sessions outside the server function so the variable is permanent
#Alternative is to write to database or file
users = reactiveValues(
logTable = data.frame(
id = character(),
login = character(),
logout = character()
)
)
server <- function(input, output, session) {
#Register session start. Isolate needed for reactive environment
isolate({
users$logTable = rbind(
users$logTable,
list(id = session$token,
login = as.character(Sys.time()),
logout = NA)
)
})
#Register session end
session$onSessionEnded(function(){
isolate({
users$logTable[users$logTable$id == session$token,
"logout"] = as.character(Sys.time())
})
})
#Display info
output$myOutput = renderTable({
users$logTable
})
}
shinyApp(ui, server)
Every time a users logs in or out, the table will be updated. Note that refreshing the page ends the session, so this simple code cannot see if a user refreshes or if the same user visits multiple times. This purely is counting sessions. The number of active sessions is the number of NA in the logout column of the table.
For a more permanent tracking you should replace the users variable with a function that writes the data to a file or database.
Shiny is a web-app and thus cannot access client system data. This would be a huge privacy issue If your app runs online, you might be able to gather the user's IP address, if they are not blocking tracking, but otherwise the only real way I know of is to provide them with a log-in screen where they enter their username that you gave them.
Also in the output you mentioned, there is no logout time capturing when the user closes the application right? But with this the main info (time the user stays on the application. Logout - Login) is not captured
This is the local user that is running the R session. When you host a app, this will then become the ID of the server, which is the same for everyone. Clients connect to your server which hosts the app.
There is logout capturing. The logout column is the time the user quits the app (or refreshes the page). You can calculate time spent by subtracting logout from login. The number of rows in the table is the total number of sessions that was started, the number of NA in the logout is the total number of active session (i.e. user has not disconnected yet)
One last tip: if your app is running online (e.g. the internet and not just locally), you can use Google analytics to get much more details on how people use your app and analyse nearly everything you like
I see. Also forgot to tell u. When the user access our shiny applications he uses pam authentication to open the application. can we use this at least to see which user is using the application??
If you just have an internal app with minimal security needs, you can just create a modal window that asks for a username and put that in your table. If you need a secure and more private way of real authentication you can look into packages like auth0.
Very useful! I have basically a Rmd file hosted in shinyapps.io. Is it possible to add it there too? Would the code look identical to the one written above? Thank you.
Thanks. I am going to try Shall I put this piece of code in one of the Rmd cells? Where would the table be showing? I wouldn't want the table showing to the users.