R Shiny应用程序中动态数量的ggvis图

时间:2015-03-05 18:31:52

标签: r shiny ggvis

它是我正在尝试构建的闪亮应用的简化版本。 我希望用户能够控制ggvis创建的绘图数量。 (在完整版中,用户还可以交互式选择要绘制的变量。) 但显然,它的回应并不是很好。

ui.r

        shinyUI(pageWithSidebar(

      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")
      )
    ))

server.r

    max_plots <- 5

    shinyServer(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="")
          ggvisOutput(plotname)
        })

        # 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="")
          cars %>% 
            ggvis(~speed, ~ dist) %>%
            layer_points() %>%
            bind_shiny(plotname)
        })
      }
    })

0 个答案:

没有答案