0

我试图创建一个搜索栏以允许用户从显示我的 Mongodb 数据库中的值的表中搜索任何值。

首先我想显示一个表中的所有值(我使用data1),然后在同一个表中我想执行搜索并显示搜索值所在的行

但我无法执行任何搜索,因为我觉得缺少某些东西

我的代码如下:

library(shiny)
library(shinySearchbar)
library(shinyWidgets)
library(mongolite)

mongo_database <- "Test"
mongo_collection <- "Test1"
url_path = sprintf("mongodb+srv://...)

shinyApp(
    ui <- fluidPage( 
        titlePanel("BoQ Data"),
        sidebarLayout(
            sidebarPanel(
                selectInput("id", "PLEASE SELECT :", choices =c("All", "Title", "Instruction", "ITEMS", "rate"),
                            selected = "All"),
                actionButton("update", "Load"),
                hr(),
                #tags$h4("Search Input"),
                
                searchInput(
                    inputId = "search", label = "Enter your text",
                    placeholder = "A placeholder",
                    btnSearch = icon("search"),
                    btnReset = icon("remove"),
                    width = "450px"
                )
            
            ),
            
            mainPanel(
                tableOutput("mydata"),
                verbatimTextOutput("res")
            )
        )
    ),
    
    
    server <- function(input, output, session) {
        
        data1 <- eventReactive(input$update, {
            mon <- mongo(collection = mongo_collection, db = mongo_database, url = url_path, verbose = TRUE)
            switch(input$id,
                   "All" = 'All',
                   "Title" = 'Title',
                   "Instruction" = 'Instruction',
                   "ITEMS" = 'ITEMS',
                   "rate" = 'rate'
            )
            
            switch (input$id,
                    All = mon$find(query = '{}'),
                    Title = mon$find(query ='{}', fields = '{"Title" : 1, "field1":1, "_id":0}'),
                    Instruction = mon$find(query ='{}', fields = '{"Instruction" : 1, "field2":1, "_id":0}'),
                    ITEMS = mon$find(query ='{}', fields = '{"ITEMS" : 1, "field3":1, "_id":0}'),
                    rate = mon$find(query ='{}', fields = '{"rate" : 1, "field4":1, "_id":0}')
            )
            #rm(mon)
        }, ignoreNULL = FALSE)
        
        #output$mydata <- renderDataTable({
        output$mydata <- renderTable({   
            data1()
            #Data1
        })
        
        output$res <- renderPrint({
            input$search
        })
        
        
        
    }
    
)

shinyApp(ui,server)


任何人都可以帮助制作搜索栏吗?

4

0 回答 0