1

I have a Shiny Application that uses a MongoDB (using mongolite). The application loads and saves to the database with no issues but I am trying to find a way to edit the MongoDB through a datatable(using DT) where when the user edits or deletes a row that they can press an actionbutton to update the mongoDB. When I try to run it currently I am getting

"Warning: Error in : argument must be bson or json."

Is there a way for me to edit from DT, convert it to the JSON Mongo is expecting from the Shiny app? Below is the code.

library(shiny)
library(DT)
library(mongolite)

ui <- fluidPage(

  # Application title
  titlePanel("MongoTest"),

  # Sidebar
  sidebarLayout(
    sidebarPanel(
      actionButton("submit", "Submit"),
      actionButton("load","Load"),
      actionButton("update","update"),
      actionButton("deleteRows", "Delete Rows")
    ),

    #Main UI
    mainPanel(
      fluidPage(
        fluidRow(h2("Interactive Table", align="center")),
        tags$hr(),
        fluidRow(
              DT::dataTableOutput("Interactive_Table")
        )
      )
    )
  )
)


server = function(input, output, session){
  #Function that loads the information from the mongoDB
  loadData <- function() {
    # Connect to the database
    db = mongo(collection = "collectionhere",db ="SET", url = "mongodb://localhost:27017")
    # Read all the entries
    data <- db$find()

    return(data)

  }

  READ_IN_DATA=loadData()

  values <- reactiveValues(dfWorking = READ_IN_DATA)

  #Function that saves data to DB
  saveData <- function(data) {
    # Connect to the database
    db = mongo(collection = "collectionhere",db ="SET", url = "mongodb://localhost:27017")
    data <- as.data.frame(t(data))
    db$insert(data)
  }

  updateData = function(data){
      # Connect to the database
      db = mongo(collection = "collectionhere",db ="SET", url = "mongodb://localhost:27017")
      data <- as.data.frame(t(data))
      #subjects$update('{}', '{"$set":{"has_age": false}}', multiple = TRUE)
      db$update(data)
  }

  #Loading In the Data
  observeEvent(input$load, {
    loadData()
  })

  #Update the DB based off changes to the table
  observeEvent(input$update, {
    updated_df=as.data.frame(values$dfWorking)
    updateData(t(updated_df))
  })

  #Deleting Rows
  observeEvent(input$deleteRows,{
    if (!is.null(input$Interactive_Table_rows_selected)) {
      values$dfWorking <- values$dfWorking[-as.numeric(input$Interactive_Table_rows_selected),]
    }
  })

  #DT Table
  output$Interactive_Table = renderDataTable({
    datatable(values$dfWorking,editable=TRUE
    )
  })

}

# Run the application 
shinyApp(ui = ui, server = server)
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
Dante Smith
  • 561
  • 1
  • 6
  • 21
  • Possible duplicate of [R : Updating an entry in mongodb using mongolite](https://stackoverflow.com/questions/37631954/r-updating-an-entry-in-mongodb-using-mongolite) – Ryan Morton Mar 27 '18 at 20:31
  • @RyanMorton thank you for the reference to that existing document but I believe it to be a bit different after looking at that. What I am trying to do is update multiple entries within the collection without it being hard coded for specific known IDs. Is that even possible? – Dante Smith Mar 30 '18 at 17:48
  • From my experience on this site, people are less like to answer questions which contain whole shiny apps with multiple functionality as they can be awkward to debug. You might get a better response if you only included one function/operation. – SymbolixAU Apr 09 '18 at 22:02
  • And it might be better to first write a functioning 'update' or 'delete' query outside of shiny. Then make a very simple shiny which just does that one function. Then add in the `DT` object and see if you can work out what's wrong. – SymbolixAU Apr 09 '18 at 22:04
  • It looks like your question just boils down to 'How can I create JSON from a DT event'. – SymbolixAU Apr 09 '18 at 22:07

0 Answers0