How can put multiple plots side-by-side in shiny r?

后端 未结 3 959
故里飘歌
故里飘歌 2020-12-13 14:29

In mainpanel, I try to handle this problem via fluidrow. However, one of my plot is optional to be displayed or not by users. When user clicks the button, the second plot ap

3条回答
  •  难免孤独
    2020-12-13 15:17

    Well, you did not exactly give us a complete example, but I think this is what you want:

    ui.r

    # ui.R
    
    shinyUI(fluidPage(
      titlePanel("title panel"),
    
      sidebarLayout(position = "left",
        sidebarPanel("sidebar panel",
          checkboxInput("do2", "Make 2 plots", value = T)
          ),
          mainPanel("main panel",
            fluidRow(
              column(6,plotOutput(outputId="plotgraph1", width="300px",height="300px")),  
              column(6,plotOutput(outputId="plotgraph2", width="300px",height="300px"))
            )
          )
        )
      )
    )
    

    server.r

    # server.r
    
    library(ggplot2)
    
    shinyServer(function(input, output) 
      {
      set.seed(1234)
      pt1 <- qplot(rnorm(500),fill=I("red"),binwidth=0.2,title="plotgraph1")
        pt2 <- reactive({
          input$do2
          if (input$do2){
            return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2,title="plotgraph2"))
          } else {
            return(NULL)
          }
        })
        output$plotgraph1 = renderPlot({pt1})
        output$plotgraph2 = renderPlot({pt2()})
      }
    )
    

    Output

    "Make 2 plots" checked:

    "Make 2 plots" unchecked:

提交回复
热议问题