【问题标题】:Shiny: subsetting a table from a textInput with multiple values闪亮:从具有多个值的 textInput 中子集表
【发布时间】:2020-08-30 23:44:42
【问题描述】:

我有一个简单的 Shiny 应用程序。用户在 textInput 中输入代码,例如:a1、b1、c1 等。

当只列出一个代码时效果很好,但如果用户编写了两个或多个用逗号分隔的代码,则效果不佳。

用户如何输入任意数量的代码?

library(shiny)

ui <- fluidPage(

  titlePanel(""),


  sidebarLayout(
    sidebarPanel(

      textInput(inputId = "textBox",
                label = "Code Search",
                placeholder = "Enter codes here seperated by a comma"),

      actionButton("textSearchButton", "Generate the Table")

    ),

    fluidRow(
      tableOutput("dtOut")
    )
  )
)

server <- function(input, output) {

  df <- data.frame(Code = paste0(letters, 1),
                   Description = "Something here",
                   Value = "Some value")

  outputFunc <- function(code, df){

    # # Dummy data
    # code <- c('a1', 'b1', 'c1')

    outTbl <- df[df$Code %in% code,]

    return(list(outTbl))
  }

  textSearch <- eventReactive(input$textSearchButton, {
    outputFunc(input$textBox, df)
  })


  output$dtOut <- renderTable({
    textSearch()[[1]]
  })

}

shinyApp(ui, server)

【问题讨论】:

    标签: shiny


    【解决方案1】:

    我稍微简化了你的代码:

    library(shiny)
    
    ui <- fluidPage(
    
      titlePanel(""),
    
    
      sidebarLayout(
        sidebarPanel(
    
          textInput(inputId = "textBox",
                    label = "Code Search",
                    placeholder = "Enter codes here seperated by a comma"),
    
          actionButton("textSearchButton", "Generate the Table")
    
        ),
    
        fluidRow(
          tableOutput("dtOut")
        )
      )
    )
    
    server <- function(input, output) {
    
      df <- eventReactive(input$textSearchButton, {
        # outputFunc(input$textBox, df)
        req(input$textBox)
        codes <- unlist(strsplit(input$textBox, ", "))
        return(data.frame(Code = codes,
                          Description = "Something here",
                          Value = "Some value"))
      })
    
      output$dtOut <- renderTable({
        df()
      })
    
    }
    
    shinyApp(ui, server)
    

    它是否满足您的需求?

    【讨论】:

    • 谢谢。我认为 textInput 的输出是一个简单的向量,而不是一个列表。
    • 这不是一个列表,它是一个字符串。
    猜你喜欢
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2017-08-21
    • 2017-06-29
    相关资源
    最近更新 更多