Skip to content

Instantly share code, notes, and snippets.

@reinholdsson
Last active June 22, 2021 21:15
Show Gist options
  • Save reinholdsson/5614910 to your computer and use it in GitHub Desktop.
Save reinholdsson/5614910 to your computer and use it in GitHub Desktop.
Shiny + mongoDB example
library(rmongodb)
shinyServer(function(input, output) {
key <- reactive({
paste(input$key, "data", sep = ".")
})
output$show <- renderText({
m <- mongo.create()
if (mongo.is.connected(m)) {
res <- mongo.find.one(m, key())
mongo.disconnect(m)
}
if (!is.null(res))
mongo.bson.value(res, "data")
})
observe({
if (input$save == 0)
return()
isolate({
m <- mongo.create()
if (mongo.is.connected(m)) {
mongo.insert(m, key(), list(data = input$value))
mongo.disconnect(m)
}
})
})
})
shinyUI(bootstrapPage(
selectInput("key", "Key:", LETTERS[1:5]),
textInput("value", "Value:"),
actionButton("save", "Save"),
textOutput("show")
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment