【问题标题】:How to pass SelectInput to startswith function?如何将 SelectInput 传递给startswith函数?
【发布时间】:2022-07-27 16:30:47
【问题描述】:

我正在尝试创建一个闪亮的应用程序,允许用户从多个数据框中选择列并在选定列中搜索值。但是我没有得到任何数据!在哪里将“input$select”作为参数传递给“startswith”函数,我所知道的“startswith”函数将字符向量作为参数来匹配字符串。 这是我的应用程序,做错了什么? 谢谢

library(shiny)
library(shinydashboard)
library(DT)

Name <- c("Martin Lawrence", "Will Smith", "Harrison Ford", "Tom Hanks")
Age <- c(55, 50, 85, 60)
Table1 <- data.frame(Name, Age, stringsAsFactors = F)
Table1 <- data.table::data.table(Table1)

Name <- c("Jim Carrey", "Mark Wahlberg", "Harrison Ford", "Tom Hanks")
Age <- c(55, 45, 85, 60)
Table2 <- data.frame(Name, Age, stringsAsFactors = F)
Table2 <- data.table::data.table(Table2)



ui <- dashboardPage(
  dashboardHeader(title = "Searching Dashboard"),
  dashboardSidebar(
    width = 300,
    sidebarMenu(
      sidebarSearchForm(
        textId = "searchText",
        buttonId = "searchButton",
        label = "Search Dataset"
      ),
      tags$br(),
      menuItem(
        "Show Data",
        tabName = "tabset3",
        icon = icon("info",
                    lib = "font-awesome")
        ),
      selectInput("select1", "Select columns to display from Table1", names(Table1), multiple = FALSE),
      tags$br(),
      selectInput("select2", "Select columns to display from Table2", names(Table2), multiple = FALSE)
    )
  ),
  dashboardBody(scrollx = TRUE,
                tabItems(
                  tabItem(tabName = "tabset3",
                          fluidRow(
                            tabBox(
                              title = "Matching Data",
                              tags$head(tags$style()),
                              id = "tabset3",
                              height = "550px",
                              width = "500px",
                              tabPanel("Table 1",
                                       div(style = 'overflow-x: scroll;', DTOutput('table1'))),
                              tabPanel("Table 2",
                                       div(style = 'overflow-x: scroll;', DTOutput('table2'))),
                              )
                          ))
                ),),
)
server <- function(input, output, session) {
  
  output$table1 <- renderDT({
    req(input$searchButton == TRUE)
    Table1[startsWith(input$select1, input$searchText)]
  })
  
  output$table2 <- renderDT({
    req(input$searchButton == TRUE)
    Table2[startsWith(input$select2, input$searchText)]
  })
}
shinyApp(ui , server)

【问题讨论】:

    标签: r shiny startswith selectinput


    【解决方案1】:

    类似这样的东西,请注意它的关键敏感Willwill是不同的搜索:

      output$table1 <- renderDT({
        req(input$searchButton == TRUE)
        Table1[startsWith(Table1[[input$select1]], input$searchText)]
      })
      
      output$table2 <- renderDT({
        req(input$searchButton == TRUE)
        Table2[startsWith(Table2[[input$select2]], input$searchText)]
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 2015-11-27
      • 2016-12-11
      • 2016-12-25
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多