如何解决如何在闪亮的应用程序中使用单个 renderPlot() 函数打印多个图?
我有一个输出多个图的应用程序。该应用程序本质上以特定形式获取一些数据,然后将其拆分,然后计算一些比例表。然后它制作这些不同表格的图表。在下面的应用程序中,我只打印了“一个”图并且它有效。当我有多个图时就会出现问题(请阅读下文)。我确实在网上找到了一些关于如何执行此操作的答案,但我未能在我的代码中实现它们。这是我的第一个闪亮的应用程序,所以我真的无法弄清楚。
library(shiny)
library(dplyr)
library(purrr)
ui<- shinyUI(fluidPage(
titlePanel(title = h4("proportion graphs",align="center")),sidebarLayout(
sidebarPanel(
),mainPanel(
plotOutput("plot"),)
)
))
server<-shinyServer(
function(input,output) {
l<- reactive({
f<- list(`0` = structure(list(X70 = "D",X71 = "C",X72 = "C",X73 = "A",X74 = "B",X75 = "C",X76 = "D",X77 = NA_character_,X78 = "B",X79 = "D",X80 = "C",Q = 1),row.names = 32L,class = "data.frame"),`1` = structure(list(X70 = c("D","B","D","D"),X71 = c("B","C",NA,"A","C"),X72 = c("A",NA),X73 = c("B",X74 = c("B",X75 = c("C",X76 = c("D","B"),X77 = c("D",X78 = c("B",X79 = c("C",X80 = c("B","A"),Q = c(2,2,1,4,3,1)),row.names = c(8L,10L,12L,17L,25L,27L,28L,33L,35L,38L,45L),`2` = structure(list(X70 = c("D",X71 = c("A",X72 = c("D",X74 = c("D",X75 = c("B",X76 = c("A",X77 = c("B",X78 = c("C",X79 = c("A",X80 = c(NA,row.names = c(4L,5L,6L,11L,15L,16L,21L,22L,26L,37L,39L,43L),`3` = structure(list(X70 = c("A",X72 = c("B",X73 = c(NA,X74 = c(NA,X75 = c(NA,X76 = c(NA,X77 = c(NA,X78 = c(NA,X79 = c(NA,2)),row.names = c(2L,13L,14L,18L,19L,20L,29L,30L,34L,36L,41L,44L),`4` = structure(list(X70 = c("D",X74 = c("C",X79 = c("D",X80 = c("C",4)),row.names = c(1L,3L,7L,9L,23L,24L,31L,40L,42L,46L,47L,48L),class = "data.frame"))
})
u<- reactive({u<- c("D","A")})
output$plot <- renderPlot({
l<-l()
u<-u()
out <- lapply(l,function(dat)
asplit(as.data.frame(t(sapply(dat,function(x)
proportions(table(factor(unlist(x),levels = u)))))),1) ) %>%
transpose %>%
map(bind_rows,.id = 'grp')
matplot(out[[1]][-1],type = "l",col = 1:4,xaxt = "n")
axis(side=1,at=1:4,labels=colnames(out[[1]][-1]))
legend("topleft",legend = colnames(out[[1]][-1]),fill = 1:4)
})
}
)
shinyApp(ui,server)
如果我尝试使用此代码打印多个图形,则不起作用。我怎样才能解决这个问题?此代码适用于控制台,但不适用于闪亮的应用
matplot(x[-1],xaxt = "n")
axis(side=1,labels=colnames(x[-1]))
legend("topleft",legend = colnames(x[-1]),fill = 1:4)
})
解决方法
用户界面
shinyUI(fluidPage(
titlePanel(title = h4("proportion graphs",align="center")),sidebarLayout( sidebarPanel( ),mainPanel(
# create a uiOutput
uiOutput("plots")
)
)
))
切断
shinyServer(
function(input,output) {
l<- reactive({
f<- list(`0` = structure(list(X70 = "D",X71 = "C",X72 = "C",X73 = "A",X74 = "B",X75 = "C",X76 = "D",X77 = NA_character_,X78 = "B",X79 = "D",X80 = "C",Q = 1),row.names = 32L,class = "data.frame"),`1` = structure(list(X70 = c("D","B","D","D"),X71 = c("B","C",NA,"A","C"),X72 = c("A",NA),X73 = c("B",X74 = c("B",X75 = c("C",X76 = c("D","B"),X77 = c("D",X78 = c("B",X79 = c("C",X80 = c("B","A"),Q = c(2,2,1,4,3,1)),row.names = c(8L,10L,12L,17L,25L,27L,28L,33L,35L,38L,45L),`2` = structure(list(X70 = c("D",X71 = c("A",X72 = c("D",X74 = c("D",X75 = c("B",X76 = c("A",X77 = c("B",X78 = c("C",X79 = c("A",X80 = c(NA,row.names = c(4L,5L,6L,11L,15L,16L,21L,22L,26L,37L,39L,43L),`3` = structure(list(X70 = c("A",X72 = c("B",X73 = c(NA,X74 = c(NA,X75 = c(NA,X76 = c(NA,X77 = c(NA,X78 = c(NA,X79 = c(NA,2)),row.names = c(2L,13L,14L,18L,19L,20L,29L,30L,34L,36L,41L,44L),`4` = structure(list(X70 = c("D",X74 = c("C",X79 = c("D",X80 = c("C",4)),row.names = c(1L,3L,7L,9L,23L,24L,31L,40L,42L,46L,47L,48L),class = "data.frame"))
})
u <- reactive({
u <- c("D","A")
})
# reactive expression to process data
out <- reactive({
l <- l()
u <- u()
lapply(l,function(dat)
asplit(as.data.frame(t(sapply(dat,function(x)
proportions(table(factor(unlist(x),levels = u)))))),1) ) %>%
transpose %>%
map(bind_rows,.id = 'grp')
})
# render UI
output$plots <- renderUI({
lapply(1:length(out()[-1]),function(i) {
# creates a unique ID for each plotOutput
id <- paste0("plot_",i)
plotOutput(outputId = id)
# render each plot
output[[id]] <- renderPlot({
x <- out()[[i]][-1]
matplot(x,type = "l",col = 1:4,xaxt = "n")
axis(side=1,at=1:4,labels=colnames(x))
legend("topleft",legend = colnames(x),fill = 1:4)
})
})
})
} )
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。