Search code examples
rmongodbshinyrmongo

JSON Parse Error in MongoDB and R


I am new to R and MongoDB and everything related to programming so please bear with me. I am trying to query a MongoDB database based on user input (dropdown menu). When I run the code, I get the following error:

Error: com.mongodb.util.JSONParseException: 
       {'Name':input$prod}
               ^

Here is my UI:

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"))
    )
))

Here is my server:

my <- mongoDbConnect("mysearch")

shinyServer(function(input, output) {
    output$table1 <- renderTable({ 
          dbGetQuery(mydb, "usercollection", "{'Name':input$prod}")
     })
}
)

Thanks so much for your help.


Solution

  • Try this...

    queryParam <- paste('{\'Name\':', input$prod, '}');
    
    shinyServer(function(input, output) {
        output$table1 <- renderTable({ 
              dbGetQuery(mydb, "usercollection", queryParam)
         })
    }
    )
    

    Instead of passing the value stored in input$prod, you are passing the string "input$prod" to the function.