我尝试创建一个闪亮的应用程序,该应用程序使用ggplot函数通过checkboxGroupInput启用用户检查的参数绘图选项。我尝试使用多重绘图功能堆叠绘图。当前,带有actionbutton的checkboxGroupInput返回一个图。谁能指出或建议我列出ggplot对象并将其堆叠在另一个之上的方法吗?以下是我的闪亮代码:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(checkboxInput("all", "Select All", FALSE),
tags$hr(),
fluidRow(
column(6,
checkboxGroupInput("checkGroup",
label = ("Parameters"),
choices = list("PHYSICAL ACTIVITY" = 1,
"SLEEP" = 2
)))),
fluidRow(
column(10, actionButton("goButton", label = "Analysis Report"))
)), #Sidebarpanel
mainPanel(
plotOutput("plot1", height='1200px'),
downloadButton(outputId = "down", label = "Download the plot"),
radioButtons(inputId = "var3", label = "Select the file type", choices = list("png", "pdf"))
)#Mainpanel
) #Sidebar layout
)
server <- function(input, output,session) {
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')
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()
})
p2 <- reactive({
if(!(2 %in% input$checkGroup)) return(NULL)
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()
})
if(input$goButton > 0)
output$plot1 <- renderPlot({
ptlist <- list(p1(),p2())
to_delete <- !sapply(ptlist,is.null)
ptlist <- ptlist[to_delete]
ggplot2.multiplot(ptlist, cols=1)
}) #Renderplot end
output$down <- downloadHandler(
filename = function(){
paste("Report", input$var3, sep=".")
},
content = function(file) {
if(input$var3 == "png")
png(file)
else
pdf(file)
ggplot2.multiplot(ptlist, cols=1)
dev.off()
}
)
} # End of server
谢谢