使用checkboxGroupInput的长度作为循环的输入来创建多个元素

使用checkboxGroupInput的长度作为循环的输入来创建多个元素,r,shiny,R,Shiny,我正在创建闪亮的应用程序,我想使用checkboxGroupInput打印出多个绘图。但是,我只想打印选中的checkboxGroupInput元素的绘图。在Shiny gallery中有一个类似的工具,可以在使用Lappy的循环中创建UI元素。下面是该示例的简化版本,以显示我想要做的事情: #server.R library(shiny) library(ggplot2) shinyServer(function(input, output, session) { numberInput

我正在创建闪亮的应用程序,我想使用checkboxGroupInput打印出多个绘图。但是,我只想打印选中的checkboxGroupInput元素的绘图。在Shiny gallery中有一个类似的工具,可以在使用Lappy的循环中创建UI元素。下面是该示例的简化版本,以显示我想要做的事情:

#server.R
library(shiny)
library(ggplot2)

shinyServer(function(input, output, session) {
  numberInput <- reactive({
    input$checkbox
  })

  lapply(1:10, function(i) {
    output[[paste0('b', i)]] <- renderPlot({
      qplot(x = rnorm(100, mean = as.numeric(numberInput()[i]))) +
        ggtitle(paste("This plot was plotted with", numberInput()[i], "option"))
    })
  })
})

#ui.R
library(shiny)    
shinyUI(fluidPage(
  title = 'lapply example',

  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("checkbox", "Checkbox",
                         choices = sample(1:10, 5))
    ),

    mainPanel(
      lapply(1:10, function(i) {
        plotOutput(paste0('b', i))
      })
    )
  )
))
然而,在所有这些情况下,Shiny都抱怨缺乏主动-被动环境


那么,我错过了什么?如何在Lappy中使用反应上下文之外的输入长度?

我通常更幸运地使用它(只是因为我更容易理解它),但想法是将绘图渲染到服务器上的UI中,然后在
UI.R

#server.R
library(shiny)
library(ggplot2)

server <- shinyServer(function(input, output, session) {
  output$checks <- renderText(input$checkbox)

  output$plots <- renderUI({
    plot_output_list <- 
      lapply(input$checkbox,
             function(i){ 
               plotOutput(paste0("plot", i))
             })
    do.call(tagList, plot_output_list)
  })

  observe({
    for (i in input$checkbox) {
      local({
        local_i <- i
        output[[paste0("plot", local_i)]] <- 
          renderPlot({
            qplot(x = rnorm(100, mean = as.numeric(local_i))) +
        ggtitle(paste("This plot was plotted with", local_i, "option"))
          })
      })
    }
  })



})

#ui.R
library(shiny)    
ui <- shinyUI(fluidPage(
  title = 'lapply example',

  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("checkbox", "Checkbox",
                         choices = sample(1:10, 5))
    ),

    mainPanel(
        verbatimTextOutput("checks"),
        uiOutput('plots')
    )
  )
))

shinyApp(ui = ui, server = server)
#server.R
图书馆(闪亮)
图书馆(GG2)
服务器
#server.R
library(shiny)
library(ggplot2)

server <- shinyServer(function(input, output, session) {
  output$checks <- renderText(input$checkbox)

  output$plots <- renderUI({
    plot_output_list <- 
      lapply(input$checkbox,
             function(i){ 
               plotOutput(paste0("plot", i))
             })
    do.call(tagList, plot_output_list)
  })

  observe({
    for (i in input$checkbox) {
      local({
        local_i <- i
        output[[paste0("plot", local_i)]] <- 
          renderPlot({
            qplot(x = rnorm(100, mean = as.numeric(local_i))) +
        ggtitle(paste("This plot was plotted with", local_i, "option"))
          })
      })
    }
  })



})

#ui.R
library(shiny)    
ui <- shinyUI(fluidPage(
  title = 'lapply example',

  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("checkbox", "Checkbox",
                         choices = sample(1:10, 5))
    ),

    mainPanel(
        verbatimTextOutput("checks"),
        uiOutput('plots')
    )
  )
))

shinyApp(ui = ui, server = server)