我是 R 和 MongoDB 以及与编程相关的一切的新手,所以请多多包涵。我正在尝试根据用户输入(下拉菜单)查询 MongoDB 数据库。当我运行代码时,出现以下错误:
Error: com.mongodb.util.JSONParseException:
{'Name':input$prod}
^
这是我的用户界面:
mydb <- mongoDbConnect("mysearch")
shinyUI(fluidPage(
titlePanel("MYsearch"),
sidebarPanel(
selectInput("prod", label = "Choose my Product/Service",
choices = list("Engineering", "Operations",
"Detection"), selected = "Engineering")
),
mainPanel(tableOutput("table1"))
)
))
这是我的服务器:
my <- mongoDbConnect("mysearch")
shinyServer(function(input, output) {
output$table1 <- renderTable({
dbGetQuery(mydb, "usercollection", "{'Name':input$prod}")
})
}
)
非常感谢您的帮助。
最佳答案
试试这个...
queryParam <- paste('{\'Name\':', input$prod, '}');
shinyServer(function(input, output) {
output$table1 <- renderTable({
dbGetQuery(mydb, "usercollection", queryParam)
})
}
)
您不是将存储在 input$prod 中的值传递给函数,而是将字符串“input$prod”传递给函数。
关于r - MongoDB 和 R 中的 JSON 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24767720/