指定不同数量的输出图表/表格(发光的应用程序)

时间:2018-10-18 09:15:13

标签: r ggplot2 shiny

我想为用户提供选择,以便他/她希望在分析结束时查看哪些图表。 所有图都来自一个数据集,包括时间序列图,箱线图,直方图等。 我偶然发现的问题是

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

Mac,任何评论将不胜感激

1 个答案:

答案 0 :(得分:1)

您可以将条件图包装在conditionalPanel中,以取消选择它们。

为此,您将需要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

enter image description here

相关问题