在两个选项卡面板中绘制相同的输出 [英] Plotting the same output in two tabPanels in shiny
本文介绍了在两个选项卡面板中绘制相同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将相同的直方图绘制在Siny中的选项卡框内的两个单独的选项卡面板中。我可以在其中一个选项卡中绘制数据,但当我为另一个选项卡添加代码时,似乎会破坏应用程序。下面是我正在尝试做的一个示例:
library(shiny)
library(dplyr)
data(mtcars)
body <- dashboardBody(
fluidRow(
tabBox(
title = "miles per gallon",
id = "tabset1", height = "250px",
tabPanel("Tab1", plotOutput("plot1")),
tabPanel("Tab2", plotOutput("plot1"), "test") # the app 'breaks' when I add in the **plotOutput("plot1")** here... however it works when I remove it
)
)
)
shinyApp(
ui = dashboardPage(
dashboardHeader(title = "Test"),
dashboardSidebar(),
body
),
server = function(input, output) {
output$plot1 <- renderPlot({hist(mtcars$mpg)})
}
)
在这个特定的示例中,我只需像这样在服务器中添加另一行
output$plot2 <- renderPlot({hist(mtcars$mpg)})
然后调用plot2,但我的实际应用程序比上面的示例稍微复杂一些,所以我想在两个选项卡中都绘制plot1。
div
当您创建一个闪亮的应用程序时,您就是在创建一个推荐答案站点,并且输出位于带有ID的div容器中。因此,您在不知道的情况下尝试创建具有相同id的两个div容器。这是行不通的。有关讨论,请参阅此处:Can multiple different HTML elements have the same ID if they're different elements?
您可以做的是将服务器代码包装在lapply()
函数中并生成两个ID:
lapply(1:2, function(nr){
output[[paste0("plot", nr)]] <- renderPlot({hist(mtcars$mpg)})
})
,然后调用plotOutput("plot1")
和plotOutput("plot2")
。也有其他可能在与conditionalPanels()
的组合中只使用一个输出,但我认为这种方式应该更适合您。
这篇关于在两个选项卡面板中绘制相同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文