How do I track user activity within a Shiny application?


You can build user tracking into a Shiny application by writing code that uses the session$user object. You can use this object to set user permissions within the application (allowing them to access certain areas of the application, or access only certain data sets, for example), or to write their activity to a log for later analysis. See below for examples and reference articles.

It's important to understand that the session$user is only populated when certain conditions are true.

  1. The server function in the Shiny app takes the session as an argument.
  2. The application is deployed to a server that is configured for authentication (e.g., RStudio Connect or Shiny Server Pro).
  3. The application requires the user to be logged in to access it.

The session$user is not populated when the application allows anonymous users, and it is not populated when you run the application from the IDE.

Simple Example

This example displays the name of the logged in user within a Shiny application.

The Code
To see the user who is logged in to an application, a few things need to be in the code:

  1. The session object has to be a parameter of the server function.
  2. The session$user has to be within a reactive component.

In this example, I've taken the sample Shiny app from the IDE (File > New File > Shiny web app) and added a few things:

  1. I added the session object to the argument list in the server function.
  2. I added a textOutput to the mainPanel in the ui object, to output the session user information.
  3. I added an element in the server (output$sessionUser) to render the user's name.

ui <- fluidPage(
    titlePanel("Old Faithful Geyser Data"),
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)


server <- function(input, output, session) {
    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')

    output$sessionUser <- renderText({
      paste("The logged in user is ", session$user, ".")

shinyApp(ui = ui, server = server)

Running the app in the IDE
When you run this app in the IDE, the session$user is not populated because you are not logged into the application. You are logged into the IDE, but not into the application itself, so the session$user object has nothing in it.

Running the app on Connect
However, when you deploy the application to the Connect server and set the Access to require login to see the application, the session$user is populated based on the login, and you can see the session user in the output.

This is just a simple example that displays the user's name, but you could use similar code to write the user's name to a log along with the activity they've performed, or restrict the user's access to certain components of an application, or tailor the data or plots based on the user.

Other examples and articles

Creating user permissions

Learn about your user with client data