【问题标题】:R shiny: Produce a table from outputs of dynamically created inputsR闪亮:从动态创建的输入的输出中生成一个表
【发布时间】:2020-12-13 11:41:43
【问题描述】:

我怎样才能为我的example_2 生成一个子表tableOutput (table_2),其输出来自selectInput 函数,该函数是从example_1 动态创建的。目前 tableOutput 的格式为[1] "" [1] ""。我还不能以类似于我的table_1 的方式以任何方式对其进行子集化。感谢您的帮助。

library(shinythemes)
library(shiny)
#rm(list = ls())

ui <-  navbarPage('Example',id = "inTabset",
                  tabPanel(title = "Example_1", value = "Example_1",
                           fluidPage(
                             tags$b( h4("Example_1", align = "left")),
                             theme = shinytheme("paper"),
                             fluidRow(
                               column(6,checkboxGroupInput("checkGroup", label ="", 
                                                           choices = c(1,2,3,4,5,6,7,8),
                                                           selected = c(1,4,7)) )
                             ),
                             br()
                           ),
                           hr(),
                           verbatimTextOutput("example1")
                  ),
                  
                  tabPanel(title = "Example_2", value = "Example_2", 
                           fluidPage( 
                             tags$b( h4("Example_2", align = "left")),
                             br(),
                             fluidRow(   
                               column(4, uiOutput("VarsInput")),
                               fluidRow(verbatimTextOutput("dataInfo")),
                               br(),
                               hr(),
                               verbatimTextOutput("example2"),
                                tableOutput("table_1"),
                               tableOutput("table_2")
                             ))
                  ))

server <- function(input, output, session) {
  
  output$example1 = renderPrint(input$checkGroup)
  
  
  output$example2 <- renderPrint(
    for (i in input$checkGroup) {
      print(input[[i]])
    }
  )
  
  K <- reactive({
    length(input$checkGroup)
  })
  
  output$VarsInput <- renderUI({
    NoV = K()
    C = sapply(1:(ceiling(NoV)), function(i){paste0(input$checkGroup[i])})
    
    output = tagList()
    for(i in seq_along(1:ceiling(NoV))){
      output[[i]] = tagList()
      output[[i]] = selectInput(C[i], C[i], c("",c("a","b","c","d","e","f")))
    }
    output
    
    
    
  })


# Table 1  ---------------------------------------------------------------
  
  table_1 <- reactive({
    table_1 = input$checkGroup
    table_1 = table_1[-3]   
    table_1
  })
  
  output$table_1 = renderTable(table_1())
  

  
# Table 2 -----------------------------------------------------------------
  
    table_2 = renderPrint(
      for (i in input$checkGroup) {
        print(input[[i]])
      }
    ) # What 'input' value will allow me to produce a subsettable table?
  
  output$table_2 = renderTable(table_2())
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以尝试另一个 reactive 表达式,就像对表 1 所做的那样。您可以将结果放入列表中,然后返回一个向量(unlist),而不是打印。

      table_2 <- reactive({
        lst <- list()
        for (i in input$checkGroup) {
          lst[[i]] <- input[[i]]
        }
        unlist(lst)
      }) 
      
    

    或者您可以有一个空向量,并将每个结果与每个输入值结合起来。

      table_2 <- reactive({
        d <- character(0)
        for (i in input$checkGroup) {
          d <- c(d, input[[i]])
        }
        d
      })
    

    请注意,现在您可以为您的 output$example2 执行此操作:

      output$example2 <- renderPrint(
        table_2()
      )
    

    根据您想要使用或显示数据的方式,有多种方法可以进一步操作以进行演示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 2016-11-21
      • 2021-10-04
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      相关资源
      最近更新 更多