r - MongoDB 和 R 中的 JSON 解析错误

标签 r mongodb shiny rmongo

我是 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/

相关文章:

java - 如何修复 CosmosDB for MongoDB api 上的 'Projection operator not supported' 错误?

R Shiny 刷新应用程序保持相同选项卡

r - 如何提取 8 个或更少单词的字符串中的第一个句子?

regex - 查找与模式匹配的所有文件名

Mongodb.aggregate() 忽略索引

javascript - 如何让 MongoDB 找到嵌套的 json 作为数组返回?

renderIUI 无法在 R 中的 Shiny 模块中工作

r - Shiny 的仪表板 - 带有复选框值的 react 条形图

r - 计算三列之间的最小差异并给出相应的值

R将向量 reshape 为多列