r - Shiny R 中的 Mongodb Query 使用响应式输入从数据库中获取数据
问题描述
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("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()
})
推荐阅读
- python - python的并发异步问题
- javascript - Bootstrap 4:当上面的部分关闭时关闭打开部分
- c++ - 按住键直到事件 C++
- python - 如何读取在记录的实际 JSON 字典部分之前具有整数值的数据文件?
- angular - chartjs-node-canvas 甜甜圈图
- ruby - 如何使用 Ruby-Vips 高效地创建纯色背景图像
- javascript - 如何通过 xpath 获取元素?
- amazon-web-services - 嘿,我如何才能看到为什么我的实时技能中的用户会出现后备意图?
- html - 如何使用 MathJax 在 Materialise 的工具提示中显示 Latex 公式?
- python - 自动播放 matplotlib FuncAnimation jsthml 元素