亲爱的R Shiny程序员,
我想将两个动态创建的绘图包含到我的仪表板中,每个绘图都放在单独的menuItem
上。我正在使用uiOutput("plot_1")
在一个menuItem
上定义第一个图,然后在另一个uiOutput("plot_2")
上使用menuItem
定义另一个图。然后,在服务器中,我正在使用output$plot_1 <- renderUI({ some code })
渲染第一个动态图,并使用output$plot_2 <- renderUI({ some code })
渲染第二个动态图。但是,只有第一个地块(plot_1)出现,并带有第二个地块的地块标题!!我应该在menuItem
内定义的ui
上显示的第二个图(plot_2)根本不显示。
你能帮我吗?
下面,我创建了可以轻松重现我的问题的代码示例:
max_plots <- 5
ui <- dashboardPage(
dashboardHeader(title = "Show dynamic plots"),
dashboardSidebar(
sidebarMenu(
menuItem("Slider 1", tabName = "tab_1", icon = icon("th")),
menuItem("Slider 2", tabName = "tab_2", icon = icon("th")),
menuItem("First Plot", tabName = "tab_3", icon = icon("line-chart")),
menuItem("Second Plot", tabName = "tab_4", icon = icon("line-chart"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "tab_1",
fluidRow(
box(title = "", status = "primary", width = 12,
sliderInput("n_1", "Number of plots 1", value=1, min=1, max=5)
)
)
),
tabItem(tabName = "tab_2",
fluidRow(
box(title = "", status = "primary", width = 12,
sliderInput("n_2", "Number of plots 2", value=1, min=1, max=5)
)
)
),
tabItem(tabName = "tab_3",
fluidRow(
box(title = "", status = "primary", width = 12,
uiOutput("plot_1")
)
)
),
tabItem(tabName = "tab_4",
fluidRow(
box(title = "", status = "primary", width = 12,
uiOutput("plot_2")
)
)
)
)
)
)
server <- function(input, output, session) {
## For dynamic plot 1
output$plot_1 <- renderUI({
plot_output_list_1 <- lapply(1:input$n_1, function(i) {
plotname_1 <- paste("plot", i, sep="")
plotOutput(plotname_1, height = 280, width = 250)
})
do.call(tagList, plot_output_list_1)
})
for (i in 1:max_plots) {
local({
my_i <- i
plotname_1 <- paste("plot", my_i, sep="")
output[[plotname_1]] <- renderPlot({
plot(1:my_i, 1:my_i,
xlim = c(1, max_plots),
ylim = c(1, max_plots),
main = paste("1:", my_i, ". n_1 is ", input$n_1, sep = "")
)
})
})
}
## For dynamic plot 2
output$plot_2 <- renderUI({
plot_output_list_2 <- lapply(1:input$n_2, function(i) {
plotname_2 <- paste("plot", i, sep="")
plotOutput(plotname_2, height = 280, width = 250)
})
do.call(tagList, plot_output_list_2)
})
for (i in 1:max_plots) {
local({
my_i <- i
plotname_2 <- paste("plot", my_i, sep="")
output[[plotname_2]] <- renderPlot({
plot(1:my_i, 1:my_i,
xlim = c(1, max_plots),
ylim = c(1, max_plots),
main = paste("1:", my_i, ". n_2 is ", input$n_2, sep = "")
)
})
})
}
}
shinyApp(ui, server)
能否请您帮助我,第一个情节在一个menuItem
上创建,第二个情节在另一个menuItem
上创建?另外,请注意,在我的原始代码中,renderPlot({})
函数对于两个图而言并不相似-动态图是不同的。