首页 > 解决方案 > 带引导程序的 Shiny 中并排图的动态数量

问题描述

我使用此示例在我的 Shiny 应用程序中生成动态数量的图。

问题是我需要这些图以响应方式并排填充,而不是在彼此下方填充:即动态使用可用的 Bootstrap 列。

我尝试在声称这样做的页面下方进一步关注评论,但我无法使其正常工作并且不遵循代码(例如,放置plotOutput并且renderPlot看起来很奇怪)。

我设法使用后一种方法使响应式网格正常工作,但图形没有出现在页面上(它们出现在 RStudio 控制台的图像窗口中)。

我已经搜索但找不到更多关于动态绘图数量的响应网格的信息。我需要使用基本图形,并且不喜欢layout用来设置整个绘图空间,因为我已经在使用layout在每个主绘图中绘制多个绘图。

这是第一个链接中用于绘制动态数字数量的代码。我希望地块与响应的行数并排填充。

max_plots <- 12

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
        headerPanel("Dynamic number of plots"),
        sidebarPanel(
            sliderInput("n", "Number of plots", value=1, min=1, max=5)
        ),
        mainPanel(
            # This is the dynamic UI for the plots
            uiOutput("plots")
        )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
    # Insert the right number of plot output objects into the web page
    output$plots <- renderUI({
        plot_output_list <- lapply(1:input$n, function(i) {
            plotname <- paste("plot", i, sep="")
            plotOutput(plotname, height = 280, width = 250)
        })
        # Convert the list to a tagList - this is necessary for the list of items
        # to display properly.
        do.call(tagList, plot_output_list)
    })
    # Call renderPlot for each one. Plots are only actually generated when they
    # are visible on the web page.
    for (i in 1:max_plots) {
        # Need local so that each item gets its own number. Without it, the value
        # of i in the renderPlot() will be the same across all instances, because
        # of when the expression is evaluated.
        local({
            my_i <- i
            plotname <- paste("plot", my_i, sep="")

            output[[plotname]] <- renderPlot({
                plot(1:my_i, 1:my_i,
                     xlim = c(1, max_plots),
                     ylim = c(1, max_plots),
                     main = paste("1:", my_i, ".  n is ", input$n, sep = "")
                )
            })
        })
    }
}

# Run the application
shinyApp(ui = ui, server = server)

欣赏任何提示

标签: rshiny

解决方案


您可以使用flowLayout而不是tagList.

在此处输入图像描述


推荐阅读