首页 > 解决方案 > 闪亮的 checkboxGroupInput ggplots 作为列表

问题描述

谁能建议我提取不同ggplots(例如geom_bar(),geom_line())的闪亮复选框组输入选项作为列表的方法。我尝试了以下简化代码,它只打印最后一个图:感谢您的帮助。

 library(shiny)
  library(ggplot2)
  library(easyGgplot2)
  patient <- cbind.data.frame(seq(1:14),matrix(sample(1:100, 84), ncol=6))
 colnames(patient) <- c('DAYS', 'PHYSICAL_ACTIVITY', 'SMOKING','ALCOHOL_INTAKE', 'HYDRATION', 'SLEEP', 'Total_score')
    ui <- fluidPage(
                titlePanel("Data Plot"),
                        sidebarLayout(
                                sidebarPanel(
                        fluidRow(column(6, 
                                    checkboxGroupInput("checkGroup", 
                                      ("Parameters"), 
                                    list("PHYSICAL ACTIVITY" = 1, 
                                       "SLEEP" = 2, 
                                      "ALCOHOL INTAKE" = 3,
                                      "SELECT ALL" = 4
                                        )))
                                      ),
                       fluidRow(column(10, actionButton("goButton", label = "Analysis Report"))
                                            )
                                          ), #Sidebarpanel
                                                    mainPanel(
                                                        plotOutput("plot1", height='800px')

                                        )#Mainpanel
                      ) #Sidebar layout
        )#fluidpage

 server <- function(input, output) {
 output$plot1 <- renderPlot({ 
 input$goButton
  p1 <- reactive({
    if(!(1 %in% input$checkGroup)) return(NULL)
      ggplot(data=patient, aes(x=DAYS, y=PHYSICAL_ACTIVITY))+geom_bar(stat="identity", aes(fill=PHYSICAL_ACTIVITY<=median(PHYSICAL_ACTIVITY)), show.legend=F)+scale_fill_manual(values = c('steelblue', 'red') )+labs(title = 'PHYSICAL ACTIVITY (STEPS)', x = NULL, y = NULL)+theme_minimal()
   })
  # Second plot
  p2 <- reactive ({
      if(!(2 %in% input$checkGroup )) return(NULL)
      p2 <- ggplot(data=patient, aes(x=DAYS,y=SLEEP))+geom_line(colour='black', size=1)+geom_point(size=3, aes(colour=cut(SLEEP,c(-Inf,summary(SLEEP)[[2]],summary(SLEEP)[[5]],Inf))), show.legend=F)+scale_color_manual(values = c("red", "orange","green"))+labs(title = 'SLEEP (hrs)', x = NULL, y = NULL) +theme_minimal() 
  })
  ptlist <- list(p1(),p2())
  ggplot2.multiplot(ptlist, cols=1)
 })
 }
shinyApp(ui, server)

标签: rggplot2shiny

解决方案


ggplot2.multiplot(ptlist, cols=1)如果替换为它将起作用do.call(ggplot2.multiplot, c(ptlist, cols=1))

但可能在反应式中使用 ggplot 函数来做到这一点并不是实现目标的好方法。你可以尝试这样的事情

library(shiny)
library(ggplot2)
library(easyGgplot2)

patient <- cbind.data.frame(
    seq(1:14), 
    matrix(
        sample(1:100, 84), 
        ncol = 6
    )
)

colnames(patient) <- c(
    'DAYS',
    'PHYSICAL_ACTIVITY',
    'SMOKING',
    'ALCOHOL_INTAKE',
    'HYDRATION',
    'SLEEP',
    'Total_score'
)

ui <- fluidPage(
    titlePanel("Data Plot"),
    sidebarLayout(
        sidebarPanel(
            fluidRow(
                column(6,
                       checkboxGroupInput(
                           "checkGroup",
                           "Parameters",
                           list(
                               "PHYSICAL ACTIVITY",
                               "SLEEP"),
                           selected = "PHYSICAL ACTIVITY")
                       )
                )
            ),
        mainPanel(
            plotOutput("plots")
        )
    )
)

server <- function(input, output) {

    plot_one <- ggplot(data = patient, aes(x = DAYS, y = PHYSICAL_ACTIVITY)) + 
        geom_bar(
            stat = "identity",
            aes(fill = PHYSICAL_ACTIVITY <= median(PHYSICAL_ACTIVITY)),
            show.legend = F) + 
        scale_fill_manual(
            values = c('steelblue', 'red')) + 
        labs(title = 'PHYSICAL ACTIVITY (STEPS)',
             x = NULL,
             y = NULL) + 
        theme_minimal()

    plot_two <- ggplot(data = patient, aes(x = DAYS, y = SLEEP)) + 
        geom_line(colour = 'black', size = 1) + 
        geom_point(size = 3, 
                   aes(colour = cut(SLEEP, 
                                    c(-Inf, 
                                      summary(SLEEP)[[2]], 
                                      summary(SLEEP)[[5]], 
                                      Inf)
                                )
                   ),
                   show.legend = F) + 
        scale_color_manual(values = c("red", "orange", "green")) + 
        labs(title = 'SLEEP (hrs)', 
             x = NULL,
             y = NULL) + 
        theme_minimal()

    list.of.plots <- list(
        `PHYSICAL ACTIVITY` = plot_one,
        `SLEEP` = plot_two
    )

    output$plots <- renderPlot(
        do.call(ggplot2.multiplot, c(list.of.plots[input$checkGroup], cols=1))
    )

}
shinyApp(ui, server)

推荐阅读