Search code examples
rshinyshiny-reactivityshinymanager

Delay/avoid duplication of shiny server side functions until after credentials


I have put together a secure app to be hosted on shinyapps.io and everything works as desired except for my reactive server functions begin to run while waiting for credentials to be entered, and then run again once the credentials are verified. This seems like it would result in additional server time and resource use in the app. Is there a way to delay the server processes until after the credentials are entered. It is set up in the same manner as the example on the shinymanager vignettes:

library(shiny)
library(shinymanager)


credentials <- data.frame(user=c("USERNAME"), 
                          password=c("PASSWORD"), 
                          stringsAsFactors = FALSE)



ui <- fluidPage(
  tags$h2("My secure application"),
  
)

# Wrap your UI with secure_app
ui <- secure_app(ui)


server <- function(input, output, session) {
  
  # call the server part
  # check_credentials returns a function to authenticate users
  res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )
  
  output$auth_output <- renderPrint({
    reactiveValuesToList(res_auth)
  })
  
  # all my server side functions here
  
}

shinyApp(ui, server)

Solution

  • We can use observeEvent, eventReactive or bindEvent to trigger functions in the server part after successful authentication:

    library(shiny)
    library(shinymanager)
    
    credentials <- data.frame(
      user = c("USERNAME"),
      password = c("PASSWORD"),
      stringsAsFactors = FALSE
    )
    
    ui <- fluidPage(
      tags$h2("My secure application"),
      verbatimTextOutput("auth_output"),
      plotOutput("myPlot")
    )
    
    # Wrap your UI with secure_app
    ui <- secure_app(ui)
    
    
    server <- function(input, output, session) {
      # call the server part
      # check_credentials returns a function to authenticate users
      res_auth <- secure_server(check_credentials = check_credentials(credentials))
      
      output$auth_output <- renderPrint({
        reactiveValuesToList(res_auth)
      })
      
      observeEvent(res_auth$user, {
        # all my server side functions here
        print("executing server functions...")
      })
    }
    
    shinyApp(ui, server)