Home > Software engineering >  R Shiny - InputPanels do not work independently/ exclusively for their plot
R Shiny - InputPanels do not work independently/ exclusively for their plot

Time:07-02

Hello :) Yesterday I made my first Shiny-App and added Inputs.

The inputs let me choose if I want to display the plot with percentages or with quantities. Here is an example:

ui <- fluidPage(
    fluidRow(
            column(width = 3,
                 selectInput("PlotChoose", "Ansicht: Proportion oder Anzahl?", choices = c("Proportion", "Anzahl"))),
            column(width = 4,
                 conditionalPanel("input.PlotChoose == 'Proportion'",
                                  column(width = 8, 
                                         plotOutput(outputId = "ex1", width = 400)
                                  )
                 ),
                 conditionalPanel("input.PlotChoose == 'Anzahl'",
                                  column(width = 8, 
                                         plotOutput(outputId = "ex2", width = 400)))),
            column(width = 5, verbatimTextOutput("t12")))
    
server <- function(input, output) {
      output$ex1 <- renderPlot({
        ggplot( survivalMutate,
                aes(x = Sex, fill = Status))  
          geom_bar(position = "fill") 
          ggtitle("Wer hat überlebt in Prozent?") 
          scale_fill_manual(values = c("grey24", "snow"))  
          labs(y= "Proportion")
      })
      
      output$ex2 <- renderPlot({
        ggplot(survivalMutate,
               aes(x = Sex,
                   fill = Status))   
          geom_bar(position = "stack")  
          ggtitle("Wer hat überlebt in Anzahl?") 
          scale_fill_manual(values = c("grey24", "snow")) 
          labs(y= "Anzahl")
      })

This seems to works great for two plots (displaying the same data, but differing in either showing percentages or the whole quantities)

The problem im having:

This is not the only plot I am displaying. My problem is, that when I change the conditionalPanel to display the percentages, EVERY plot in the whole app that has the conditionalPanel will change (vice versa for clicking on "quantities"). How can I make the InputPanels independent, so that when I change one of the panels it does not affect every plot in the app that is using the same function?

To further understand: here is some more code (2 additional plots) and a screenshot of my app to visualize the problem:

ui <- fluidPage(
    fluidRow(
            column(width = 3,
                 selectInput("PlotChoose", "Ansicht: Proportion oder Anzahl?", choices = c("Proportion", "Anzahl"))),
            column(width = 4,
                 conditionalPanel("input.PlotChoose == 'Proportion'",
                                  column(width = 8, 
                                         plotOutput(outputId = "ex1", width = 400)
                                  )
                 ),
                 conditionalPanel("input.PlotChoose == 'Anzahl'",
                                  column(width = 8, 
                                         plotOutput(outputId = "ex2", width = 400)))),
            column(width = 5, verbatimTextOutput("t12"))),

h4("____________________________________________________________________________________________________________________________________________"),
      
      fluidRow(
        column(width = 3,
               selectInput("PlotChoose", "Ansicht: Proportion oder Anzahl?", choices = c("Proportion", "Anzahl"))),
        column(width = 4,
               conditionalPanel("input.PlotChoose == 'Proportion'",
                                column(width = 8, 
                                       plotOutput(outputId = "ex3", width = 400)
                                )
               ),
               conditionalPanel("input.PlotChoose == 'Anzahl'",
                                column(width = 8, 
                                       plotOutput(outputId = "ex4", width = 400)))),
        column(width = 5, verbatimTextOutput("t34")))



server <- function(input, output) {
      output$ex1 <- renderPlot({
        ggplot( survivalMutate,
                aes(x = Sex, fill = Status))  
          geom_bar(position = "fill") 
          ggtitle("Wer hat überlebt in Prozent?") 
          scale_fill_manual(values = c("grey24", "snow"))  
          labs(y= "Proportion")
      })

      output$ex2 <- renderPlot({
        ggplot(survivalMutate,
               aes(x = Sex,
                   fill = Status))   
          geom_bar(position = "stack")  
          ggtitle("Wer hat überlebt in Anzahl?") 
          scale_fill_manual(values = c("grey24", "snow")) 
          labs(y= "Anzahl")
      })

  output$ex3 <- renderPlot({
    ggplot(survivalMutate, 
           aes(x = Pclass,
               fill = Status))   
      geom_bar(position = "fill")  
      ggtitle("Mehr tot in welchen Klassen in %?") 
      scale_fill_manual(values = c("grey24", "snow")) 
      labs(y= "Proportion")
  })
  
  output$ex4 <- renderPlot({
    ggplot(survivalMutate, 
           aes(x = Pclass,
               fill = Status))   
      geom_bar(position = "stack") 
      ggtitle("Mehr tot in welchen Klassen in anzahl? ") 
      scale_fill_manual(values = c("grey24", "snow")) 
      labs(y= "Anzahl")
  })

As you can see every InputPanel has chosen "Proportion" and is displaying them

But as soon as I change the first InputPanel to "Anzahl" (quantities) every plot changes:

enter image description here

I need them to be independent - any ideas?

If you've read until here thank you a lot and have a great day/evening/night - thank you so much!

CodePudding user response:

You should't use the same input twice. Just give it another inputId, this should solve your problem. Below I just renamed the input for the bottom plot to PlotChoose2.

I can't test, since you didn't include your data survivalMutate. It is best practice to include this or an excerpt of your data so that we can reproduce the problem.

ui = fluidPage(
  
  fluidRow(
    
    column(width = 3,
           selectInput("PlotChoose", "Ansicht: Proportion oder Anzahl?", choices = c("Proportion", "Anzahl"))),
    
    column(width = 4,
           conditionalPanel("input.PlotChoose == 'Proportion'",
                            column(width = 8, 
                                   plotOutput(outputId = "ex1", width = 400)
                            )
           ),
           conditionalPanel("input.PlotChoose == 'Anzahl'",
                            column(width = 8, 
                                   plotOutput(outputId = "ex2", width = 400)))),
    column(width = 5, verbatimTextOutput("t12"))),
  
  h4("____________________________________________________________________________________________________________________________________________"),
  
  fluidRow(
    column(width = 3,
           selectInput("PlotChoose2", "Ansicht: Proportion oder Anzahl?", choices = c("Proportion", "Anzahl"))),
    column(width = 4,
           conditionalPanel("input.PlotChoose2 == 'Proportion'",
                            column(width = 8, 
                                   plotOutput(outputId = "ex3", width = 400)
                            )
           ),
           conditionalPanel("input.PlotChoose2 == 'Anzahl'",
                            column(width = 8, 
                                   plotOutput(outputId = "ex4", width = 400)))),
    column(width = 5, verbatimTextOutput("t34"))))
  • Related