Restrict SelectInput Based on Session User

Hello. I am new to shiny and I am currently trying to construct an app to visualize worker performance over time. I am trying to create a password protected dashboard where users marked as managers can view all other user stats, and non-manager users can only view their own stats. However I cannot get it to work. Here is what I have so far. The users list is employees matched with their username (2 variables) , and the credentials list is usernames and passwords (2 variables). The issue is somewhere with the observe function imo. If the user is not manager I want their selectinput button locked on their own name. ie. the user can only enter: session$user==users$user in selectinput.

The app immediately closes upon opening with the following error:

Warning: Error in if: argument is of length zero.

I know this means the if statement is taking in something NULL, however I cannot fix it after much time trying.

Any guidance would be much appreciated.

library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(plotly)
library(rsconnect)
library(shinymanager)

data<-read.csv("Data/data.csv")
data$Create.Date<-as.Date(data$Create.Date)
users<-read.csv("Data/users.csv")
credentials<-unique(read.csv("Data/credentials.csv"))

inactivity <- "function idleTimer() {
var t = setTimeout(logout, 120000);
window.onmousemove = resetTimer; // catches mouse movements
window.onmousedown = resetTimer; // catches mouse movements
window.onclick = resetTimer;     // catches mouse clicks
window.onscroll = resetTimer;    // catches scrolling
window.onkeypress = resetTimer;  //catches keyboard actions

function logout() {
window.close();  //close the window
}

function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 300000);  // time is in milliseconds (1000 is 1 second)
}
}
idleTimer()"

  ui<-secure_app(head_auth=tags$script(inactivity),
                 dashboardPage(
    
    dashboardHeader(title = "Services Dashboard"),
    
    dashboardSidebar(
      selectInput("name","Select a User", choices=""),
      dateRangeInput("date", "Select a Date Range",format="mm-dd-yy"),
      actionButton("go", "Go")
    ),
    
    dashboardBody(
      plotlyOutput("plot"),
      tableOutput("table"),
    )
  )
  )
  
  server<-function(input, output, session){
    
    res_auth<-secure_server(check_credentials = check_credentials(credentials))
    
    user<-reactive({
      session$user
    })
    
    manager<-reactive({
      if(user()=="manager"){
        return(TRUE)
      }else{
        return(FALSE)
      }
    })
    
    observe({
    if(manager()){
        updateSelectInput(session, "names", choices=users[,1]
        )
      }else{
        updateSelectInput(session, "names", choices=users$user[users$username==user()]
        )}
    })
    
    masterdata<-eventReactive(input$go, {
      data %>%
        filter(
          as.Date(Create.Date) >= as.Date(input$date[1]),
          as.Date(Create.Date) <= as.Date(input$date[2]),
          Staff.Created == input$name)
      })
    
    aggdata<-eventReactive(input$go, {
      data %>%
        filter(
          as.Date(Create.Date) >= as.Date(input$date[1]),
          as.Date(Create.Date) <= as.Date(input$date[2]),
          Staff.Created == input$name)%>%
        summarise(`Services Provided in Period Selected`=sum(count))
        
    })
    
    output$plot<-renderPlotly({
      ggplot(masterdata(), 
             aes(x=Create.Date, y=count, label=count),
             xmin=input$date[1], xmax=input$date[2], ymin=0, fill = input$date)+
        xlab("Date")+
        ylab("Services Provided")+
        geom_line(group=1, colour="#000099")+
        theme(axis.text.x = element_text(angle=45, vjust=0.5, size=8))+
        scale_x_date(breaks = "days", date_labels = "%m.%d")+
        geom_point()
        })
    
    output$table<-renderTable({
      aggdata()
    })
    
  }
  
shinyApp(ui = ui, server = server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.