0

I have been troubleshooting with this for a long time now and can't figure out why my UI isn't changing based on the two variables selected in the second sidebar panel. When I select two variables in the second sidebar panel, I want a scatterplot to show up in the main panel. After running the document, I see that the sidebar panel is added, but when I select variables and click the 'Generate Scatterplot' button, nothing shows up in the main panel.

Here's my code

shinydata <- s3read_using(FUN = read.csv,
                     bucket = "ddsproject1",
                     object = "CaseStudy2-data.csv")
ui <- fluidPage(
  
  titlePanel("Employee Attrition"),
  
  # summary plots of employee attrition 
  
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30),
      
      radioButtons(inputId = "plotType",
                   label = "Choose variable:",
                   choices = c("Job Level", "Age", "Environment Satisfaction", "Monthly Income", "Distance from Home"),
                   selected = "Job Level"),

    ),
    
    mainPanel(
      conditionalPanel(
        condition = "input.plotType == 'Job Level'",
        plotOutput(outputId = "JobLevel")
      ),
      conditionalPanel(
        condition = "input.plotType == 'Age'",
        plotOutput(outputId = "Age")
      ),
      conditionalPanel(
        condition = "input.plotType == 'Environment Satisfaction'",
        plotOutput(outputId = "EnvironmentSatisfaction")
      ),
      
      conditionalPanel(
        condition = "input.plotType == 'Monthly Income'",
        plotOutput(outputId = "MonthlyIncome")
      ),
      
      conditionalPanel(
        condition= "input.plotType == 'Distance from Home'",
        plotOutput(outputId = "DistanceFromHome")
      ),
    ) 
  ),
  
  # scatterplots of employee attrition 
  
  
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "variable1",
                  label = "Select Variable 1:",
                  choices = c("Job Level", "Age", "Environment Satisfaction", "Monthly Income", "Distance from Home")),
      selectInput(inputId = "variable2",
                  label = "Select Variable 2:",
                  choices = c("Job Level", "Age", "Environment Satisfaction", "Monthly Income", "Distance from Home")),
      actionButton(inputId = "scatterPlotButton", label = "Generate Scatterplot")
    ),
    mainPanel(
      plotOutput(outputId = "scatterplot")
      #uiOutput(outputId="scatterplot")
    )  
  )
)

# server -----------

server <- function(input, output) {
  
  output$JobLevel <- renderPlot({
    x <- shinydata$JobLevel
    
    if (length(x) == 0 || all(is.na(x))) {
      return(NULL)
    }
    
    bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = input$bins + 1)
    
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Job Level",
         main = "Histogram of Job Level of Employee Attrition")
  })
  
  output$Age <- renderPlot({
    x <- shinydata$Age
    
    if (length(x) == 0 || all(is.na(x))) {
      return(NULL)
    }
    
    bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = input$bins + 1)
    
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Age",
         main = "Histogram of Age of Employee Attrition")
  })
  
  output$EnvironmentSatisfaction <- renderPlot({
    x <- shinydata$EnvironmentSatisfaction
    
    if (length(x) == 0 || all(is.na(x))) {
      return(NULL)
    }
    
    bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = input$bins + 1)
    
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Environment Satisfaction",
         main = "Histogram of Environment Satisfaction of Employee Attrition")
  })
  output$MonthlyIncome <- renderPlot({
    x <- shinydata$MonthlyIncome
    
    if (length(x) == 0 || all(is.na(x))) {
      return(NULL)
    }
    
    bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = input$bins + 1)
    
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Monthly Income",
         main = "Histogram of Monthly Income of Employee Attrition")
  })

  output$DistanceFromHome <- renderPlot({
    x <- shinydata$DistanceFromHome
    
    if (length(x) == 0 || all(is.na(x))) {
      return(NULL)
    }
    
    bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = input$bins + 1)
    
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Distance from Home",
         main = "Histogram of Employees' Distance From Home of Employee Attrition")
  })
  
  # for second panel 
  
  observeEvent(input$scatterPlotButton, {
    variable1 <- input$variable1
    variable2 <- input$variable2
    
  #print(paste("Variable 1:", variable1))
  #print(paste("Variable 2:", variable2))
  
    
    #if (!is.null(variable1) && !is.null(variable2)) {
      x <- shinydata[[variable1]]
      y <- shinydata[[variable2]]
      
      if (!all(is.na(x)) && !all(is.na(y))) {
        scatterplot <- ggplot(data = shinydata, aes(x = x, y = y)) +
          geom_jitter() +
          labs(x = variable1, y = variable2, title = "Scatterplot of Two Variables")
        
        print(scatterplot)
        output$scatterplot <- renderPlot({ scatterplot })
      #}
    }
  })
}

shinyApp(ui, server)

Some of the debugging I've tried: checking syntax, printing out and making sure the selected variables are being read correctly, reloading libraries (specifically, ggplot2).

  • Not reproducible. What is `shinydata`? – Jan Aug 12 '23 at 17:04
  • 1
    @Jan is right. We need to see `shinydara`. Without that we can’t test. It, first observations: you have two `mainPanel`s in your UI. That looks odd. And you know you can do all this without any `conditionalPanel`s, don’t you? – Limey Aug 12 '23 at 17:21
  • See, for example, my answer [here](https://stackoverflow.com/questions/71810703/how-to-connect-user-input-with-gganimate-graph-in-r-shiny/71815357#71815357). – Limey Aug 12 '23 at 17:26
  • @Jan @Limey Sorry, pretty new to Rshiny ~ `shinydata` is a dataset containing observations with the variables `attrition, joblevel, age, environmentsatisfaction, monthlyincome, and distancefromhome`. I updated the question to include where the dataset was read in from (is that what is needed?). I was not aware of conditionalPanels ~ thanks – Catherine Ticzon Aug 12 '23 at 17:42
  • I see no update. We don’t need to see where `shinydata` comes from. We need to see _what it contains_. If its contents are confidential or proprietary, reformulate your question to use a publicly available dataset such as `mtcars` or `diamonds`. Also, I am confident that the answer I linked to above will give you the techniques you need to do what I think you want to do much more simply, flexibly and robustly than your current approach. – Limey Aug 12 '23 at 17:54
  • @Limey yes, just worked through the example you referred to! So much simpler. Thank you so much!! – Catherine Ticzon Aug 12 '23 at 18:07
  • Does this answer your question? [How to connect user input with gganimate graph in R shiny?](https://stackoverflow.com/questions/71810703/how-to-connect-user-input-with-gganimate-graph-in-r-shiny) – Limey Aug 12 '23 at 18:45

0 Answers0