【问题标题】:Mongodb Query in Shiny R to fetch the data from the database using reactive inputShiny R 中的 Mongodb Query 使用响应式输入从数据库中获取数据
【发布时间】:2018-10-06 14:57:43
【问题描述】:
library(shiny)
library(mongolite)

ui <- fluidPage( 
    titlePanel("Mongodb Data"),
    sidebarLayout(
        sidebarPanel(
             textInput("_id", "Document type:", "")
        ),
        mainPanel(
            dataTableOutput("mydata")
        )
    )
)

server <- function(input, output) {
    mon <- mongo(collection = "collectionname", db = "db name", url = "mongodb://localhost:27017")
    output$mydata <- renderDataTable({
        doc_type <- paste0(doc_type= input$doc_id)

        mon$find(  query = '{"doc_type" : {"$in" : ["x", "y"]} }' , limit = 100) 
    })
}
}

警告:错误,如果没有活动的反应上下文,则不允许操作。 (你试图做一些只能在反应式表达式或观察者内部完成的事情。)

如何添加响应式查询并从 MongoDB 集合中的特定列检索数据?每当我给文本输入值 x 或 y 时,它都应该显示来自 MongoDB 数据库的相关文档。

【问题讨论】:

  • 您拥有textInput("_id",...),但随后拥有input$doc_id。这是“_id”与“doc_id”不同的错字吗?
  • 是的,这是一个拼写错误。我很抱歉这个错误。 @MrFlick

标签: r mongodb shiny


【解决方案1】:

您需要根据用户输入进行更改的查询。尝试将您的服务器代码更改为此。我正在考虑您的问题中有错字,您的实际输入是 textInput("doc_id", "Document type:", "")

服务器:

  mon <- mongo(collection = "collectionname", db = "db name", url = "mongodb://localhost:27017")

  # Create a reactive element. Changes when the user input changes
  data.for.table <- reactive({
    # Build a query that concatenates value of input$doc_id to other strings
    query.foo <- paste0('{"doc_type" : {"', input$doc_id, '" : ["x", "y"]} }' )
    # Retrieve data
    mon$find(query = query.foo, limit = 100)
  })

  output$mydata <- renderDataTable({
    data.for.table()
  })

【讨论】:

  • 我尝试使用上面的代码。它抛出一个错误:(警告:错误:无效过滤器:空键)@ Vishesh Srivastav
  • 以上代码适用于 selectInput() 但不适用于 textInput()
猜你喜欢
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 2021-04-17
  • 2019-12-19
  • 1970-01-01
  • 2019-09-08
  • 2017-12-17
相关资源
最近更新 更多