Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R Shiny – InputPanels do not work independently/ exclusively for their plot

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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"))))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading