【问题标题】:Specifying different number of output plots/tables (Shiny app)指定不同数量的输出图/表(闪亮的应用程序)
【发布时间】:2019-03-23 01:41:09
【问题描述】:

我想让用户选择他/她想在分析结束时查看哪些图/表。 所有的图都是从一个数据集生成的,包括时间序列图、箱线图、直方图等。 我偶然发现的问题是

  1. 我是使用一个还是多个plotOutput("Plot",....) 元素?到目前为止,我一直在一个图中安排图,所以一个 plotOutput 就足够了
  2. 我是否使用预定义的高度,如plotOutput("Plot",height = "1800px")? 如果数字的数量不同,这会产生空白,我想避免它。
  3. 如何添加带有结果的表格?

任何 cmets 将不胜感激,Mac

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    您可以将绘图包装在条件面板中以取消选择它们。

    为此,您需要 1. 多个 plotOutput。 2.当所有东西都包裹在一个fluidRow中时,就不会有任何空白空间。 3.看下面的例子和:http://shiny.rstudio.com/reference/shiny/0.14/tableOutput.html

    library(shiny)
    library(shinydashboard)
    library(shinyWidgets)
    
    ui <- dashboardPage(
      dashboardHeader(title = "Plot selection"),
      dashboardSidebar(
        materialSwitch(inputId="switch1", label = "Show plot 1", value = TRUE, status = "primary"),
        materialSwitch(inputId="switch2", label = "Show plot 2", value = TRUE, status = "primary"),
        materialSwitch(inputId="switch3", label = "Show plot 3", value = TRUE, status = "primary"),
        materialSwitch(inputId="switch4", label = "Show plot 4", value = TRUE, status = "primary")
      ),
      dashboardBody(
        # Boxes need to be put in a row (or column)
        fluidRow(
          conditionalPanel(condition = "input.switch1", box(plotOutput("plot1", height = 250))),
          conditionalPanel(condition = "input.switch2", box(plotOutput("plot2", height = 250))),
          conditionalPanel(condition = "input.switch3", box(plotOutput("plot3", height = 250))),
          conditionalPanel(condition = "input.switch4", box(plotOutput("plot4", height = 250))),
          column(12,
                 dataTableOutput('table')
          )
        )
      )
    )
    
    server <- function(input, output) {
    
      df <- data.frame(col1 = rnorm(500), col2 = rnorm(500), col3 = rnorm(500), col4 = rnorm(500))
    
    output$plot1 <- renderPlot({
    plot(df$col1, col="red", main="Plot 1")
      })
    
    output$plot2 <- renderPlot({
      plot(df$col2, col="green", main="Plot 2")
    })
    
    output$plot3 <- renderPlot({
      plot(df$col3, col="blue", main="Plot 3")
    })
    
    output$plot4 <- renderPlot({
      plot(df$col4, col="black", main="Plot 4")
    })
    
    output$table <- renderDataTable(df)
    
    }
    
    shinyApp(ui, server)
    

    编辑 ----------------------------------------

    这是一个纯粹的闪亮版本:

    library(shiny)
    
    ui <- fluidPage(
    
      titlePanel("Plot selection"),
    
      sidebarLayout(
        sidebarPanel(width = 2,
                     checkboxInput(inputId="switch1", label = "Show plot 1", value = TRUE),
                     checkboxInput(inputId="switch2", label = "Show plot 2", value = TRUE),
                     checkboxInput(inputId="switch3", label = "Show plot 3", value = TRUE),
                     checkboxInput(inputId="switch4", label = "Show plot 4", value = TRUE)
        ),
    
        mainPanel(
          fluidRow(
            conditionalPanel(condition = "input.switch1", plotOutput("plot1", height = 250)),
            conditionalPanel(condition = "input.switch2", plotOutput("plot2", height = 250)),
            conditionalPanel(condition = "input.switch3", plotOutput("plot3", height = 250)),
            conditionalPanel(condition = "input.switch4", plotOutput("plot4", height = 250)),
            column(12,
                   dataTableOutput('table')
            )
          )
        )
      )
    )
    
    server <- function(input, output) {
    
      df <- data.frame(col1 = rnorm(500), col2 = rnorm(500), col3 = rnorm(500), col4 = rnorm(500))
    
      output$plot1 <- renderPlot({
        plot(df$col1, col="red", main="Plot 1")
      })
    
      output$plot2 <- renderPlot({
        plot(df$col2, col="green", main="Plot 2")
      })
    
      output$plot3 <- renderPlot({
        plot(df$col3, col="blue", main="Plot 3")
      })
    
      output$plot4 <- renderPlot({
        plot(df$col4, col="black", main="Plot 4")
      })
    
      output$table <- renderDataTable(df)
    
    }
    
    # shinyApp(ui, server)
    
    shinyApp(ui = ui, server = server)
    

    欲了解更多信息,请参阅:

    https://rstudio.github.io/shinydashboard/get_started.html https://dreamrs.github.io/shinyWidgets/reference/materialSwitch.html

    【讨论】:

    • 谢谢,会试试的。希望它可以与我目前基于 sidebarLayout/Panel 的应用程序的其余部分一起使用。很可能我必须在闪亮仪表板中重建它,对吗?
    • 不,shinydashboard 只是个人喜好 - 请参阅我的编辑。
    • 不幸的是,纯闪亮的代码对我不起作用,我收到错误:“盒子中的错误(plotOutput(“plot1”,height = 250)):plot.new 没有被调用还”
    • 两次调用shinyApp(复制、粘贴)。现在运行正常吗?
    • 呃..我忘了从闪亮仪表板中删除“框”-功能。查看另一个编辑
    猜你喜欢
    • 2018-11-09
    • 2019-03-24
    • 2014-04-11
    • 2021-12-11
    • 2020-10-27
    • 2016-12-09
    • 2023-03-07
    • 2017-07-10
    • 2020-11-05
    相关资源
    最近更新 更多