0

I have been reading several posts on here about conditional panels in R shiny, and while I can recreate their simple examples, I cannot get it to work on my app.

I wish to have 2 tabs (named "Availability" and "Disposal" in example below), with different main panels. In particular, for the second tab, I'd like the main panel to be conditional on the variable input. If input.variable2=='Raw_tot_manuf' display 2 plot2 and plot2b, for other values display plot2 only and some text. The main panel for the first tab will be yet again different.

Based on previous posts/solutions, I have come up with the following code, but now none of the main panels (for both tabs) display anything (they did before the insertion of the conditional statement).

It will be probably obvious from the code below but this is my first attempt at building a Shiny app...

   ui <-
  navbarPage(
    "My Application",
    tabsetPanel(id = "tabs",
                tabPanel(
                  "Availability of raw milk",
                  sidebarLayout(sidebarPanel(
                    selectInput(
                      "variable",label = "Availability of raw milk",
                      list("UK production" = "UK_prod",
                           "Imports" = "Imports",
                           "Total available" = "Total"))),

                    mainPanel(plotOutput("plot_1")))
                ),

                tabPanel("Disposal of raw milk",
                         sidebarLayout(
                           sidebarPanel(selectInput(
                             "variable2",label = "Disposal of raw milk",
                             list("Liquid milk" = "Raw_liq_milk",
                                  "For manufacture" = "Raw_tot_manuf",
                                  "Total available" = "Raw_exports",
                                  "Stock change & wastage" = "Raw_wastage"))),

                           mainPanel(
                             conditionalPanel(condition = "input.tabs =='Disposal of raw milk' & input.variable2=='Raw_tot_manuf'",
                                              plotOutput("plot2"),plotOutput("plot2b")),
                             conditionalPanel(condition = "input.tabs =='Disposal of raw milk' & input.variable2!='Raw_tot_manuf'",
                                              plotOutput("plot2"),textOutput("Nothing here")))))))

server<-
  function(input, output) {
    formulaText1 <- reactive({
      paste(input$variable,"~Year")})

    formulaText2 <- reactive({
      paste(input$variable2,"~Year")})


    manuf_data<- reactive ({
      if (input$variable2=="Raw_tot_manuf")
        melt(milk_data[,c(1,7:13)],id.vars=c("Year"),variable.name="Product", value.name="million_L")})

    output$plot1<-  renderPlot({
      plot(as.formula(formulaText1()))}) 

    output$plot2 <-  renderPlot({
      plot(as.formula(formulaText2()))}) 

    output$plot2b <-  renderPlot({
      p<- ggplot( manuf_data(), aes(x=Year, y=million_L, fill=Product)) + geom_area()
      print(p)}) 
  }
Community
  • 1
  • 1
Flavie
  • 3
  • 3

1 Answers1

1

You cannot output the same thing twice ( plotOutput("plot2") ). Then it always breaks down. You need to put the extra condition into the plot function. fx.

output$plot2b <- renderPlot(if (input$variable2=="Raw_tot_manuf") {plot(as.formula(formulaText2()))} else {})
mtcarsalot
  • 301
  • 2
  • 8