NewBee
NewBee

Reputation: 1040

Shiny: Create multiple tabs with separate inputs on each

I am building a shiny application with several tabs, each tab takes a user input (unique(data_in$cat), and generates some type of graph. The problem occurs in the second tab--for some reason, it does not generate the graph that is specified by data2. The first graph on the first tab is being displayed correctly.I see no error when I run this code, so I don't know where to start debugging!

library(shiny)
library(openxlsx)
library(ggplot2)


data_in <- read.xlsx("www/dem_data_clean.xlsx")



ui <- navbarPage(title = "Data",
                 tabPanel(title = "Over-all trends",
                          plotOutput("Histall"),
                          selectInput("Indall","Demographic Variable of Interest",choices = unique(data_in$cat))
                 ),
                 tabPanel(title = "2017-2018"),
                 plotOutput("Hist17"),
                 selectInput("Ind17","Demographic Variable of Interest",choices = unique(data_in$cat))
)






server <- function(input, output, session) {
  
  data1 <- reactive({
    a <- subset(data_in,cat==input$Indall)
    return(a)
  })
  
  data2 <- reactive({
    a <- subset(data_in,cat==input$Ind17)
    return(a)
  })
  
  
  output$Histall <- renderPlot({
    ggplot(data1(), aes(x=Year,y=value, group =name, color=name)) + geom_line(stat = "identity") +
      ylab("Percent of Population") 

    
  })
  
  output$Hist17 <- renderPlot({
    data2() %>%
      filter(Year=="2017-18") %>%
      ggplot(aes(name, value)) + geom_bar(stat = "identity")

    
  })
  
  
  
  
}

shinyApp(ui, server)

Any suggestions to what I am doing wrong? I've tried playing around with different things for a few hours now to no avail!

Upvotes: 1

Views: 1415

Answers (1)

HubertL
HubertL

Reputation: 19544

The UI code is not correct, second plotOutput and selectInput are not within second tabPanel. It works if you fix it :

ui <- navbarPage(title = "Data",
                 tabPanel(title = "Over-all trends",
                          plotOutput("Histall"),
                          selectInput("Indall",
                                      "Demographic Variable of Interest",
                                      choices = unique(data_in$cat))
                 ),
                 tabPanel(title = "2017-2018",
                          plotOutput("Hist17"),
                          selectInput("Ind17",
                                      "Demographic Variable of Interest",
                                      choices = unique(data_in$cat)))
)

Upvotes: 2

Related Questions