【问题标题】:How do I add search widget to my shiny app?如何将搜索小部件添加到闪亮的应用程序?
【发布时间】:2019-04-25 11:42:26
【问题描述】:

我希望能够在我的应用程序中添加一个搜索小部件,链接:https://aquayaapps.shinyapps.io/big_data/ 这样当我输入任何单词(即水)时,它就会输出所有包含水的单词。

到目前为止,我只能添加搜索小部件,但我不知道如何包含交互性,例如当我在搜索栏上键入时,会显示结果。

sidebarSearchForm(textId = "searchText", buttonId = "searchButton",label = "Search dataset",
                        icon = shiny::icon("search"))

这是 UI,但我不知道如何使它具有交互性。

搜索栏应该是交互式的,这样当我输入一个单词时,它就会输出搜索结果。

【问题讨论】:

    标签: r shiny shiny-reactivity


    【解决方案1】:

    这有点取决于你想要什么输出,但原理是一样的。我假设您有某种数据表,并且您想要包含搜索词的任何行。

    因此你需要:

    • 由输入过滤的表或数据表的输出(在本例中为 input$searchText
    • req() 如果您希望它仅在您按下搜索时显示 按钮

    这是一个非常丑陋的模型,但希望你能明白。

    library(shiny)
    library(shinydashboard)
    library(data.table)
    
    header <- dashboardHeader(title = "Search function")
    
    sidebar <- dashboardSidebar(
      sidebarSearchForm(textId = "searchText", buttonId = "searchButton", 
                        label = "Search dataset", icon = shiny::icon("search"))
    )
    
    body <- dashboardBody(tableOutput("filtered_table"))
    
    ui <- dashboardPage(title = 'Search', header, sidebar, body)
    
    
    server <- function(input, output, session) {
    
          example_data <- data.table(ID = 1:7, word = c("random", "words", "to", 
                                                        "test", "the", "search", "function")) 
    
          output$filtered_table <- renderTable({
            req(input$searchButton == TRUE)
            example_data[word %like% input$searchText]
          })
    
        }
    
        shinyApp(ui = ui, server = server)
    

    编辑:只是补充一下,如果您确实希望用户可以搜索的数据表可见,如果您使用包DT 中的dataTableOuputrenderDataTable,其中包括表的搜索功能。

    【讨论】:

    • 我想添加一个带有元数据的数据表,其中包含指向网站的链接,当单击时在新选项卡中打开这些链接。最大的挑战是如何在数据表中的单词中包含指向网站的超链接?
    • 这是一个单独的问题。这个答案是否解决了您最初的问题?这是与您的新问题相关的现有答案:stackoverflow.com/questions/28117556/…
    猜你喜欢
    • 2020-04-04
    • 2017-12-30
    • 2015-06-15
    • 2017-07-27
    • 2016-11-16
    • 2020-09-12
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    相关资源
    最近更新 更多