闪亮的mongolite查询

时间:2016-01-14 15:31:36

标签: r shiny mongolite

我想在Shiny中的反应上下文中运行mongolite查询。 db连接参数已预先加载,未在下面显示。 (注意:loadData函数出现在第一个代码块中)。查询在反应性上下文之外运行得很好。

我似乎无法在网上找到一个与Shiny和mongolite一起工作的工作示例。任何帮助将不胜感激。在下面的简化示例中,我只是想让Shiny返回查询结果中的行数。

由于

loadData <- function() {
  # Connect to the database
  db <- mongo(collection = collectionName,
              url = sprintf(
                "mongodb://%s:%s@%s/%s",
                options()$mongodb$username,
                options()$mongodb$password,
                options()$mongodb$host,
                databaseName))
  # Read all the entries
  data <- db$find(query)
  data
}


server <- function(input, output) {

   queryResults <- reactive({

    ########----------BRING IN USER QUERY PARAMETERS----------########
    var1 <- "satisfaction"
    var2 <- "performance"
    var1path <- 20072
    var2path <- 30033

    ########----------DEFINE QUERY PARAMETERS----------########
    var1 <- as.character(var1)
    var1 <- paste(var1, collapse = "|")
    var1JSON <- toJSON (var1)

    var2 <- as.character(var2)
    var2 <- paste(var2, collapse = "|")
    var2JSON <- toJSON (var2)

    #-----Creating JSONs for taxonomy codes

    var1path <- as.character(var1path)
    var1path <- paste(var1path, collapse = "|")
    var1path <- toJSON (var1path)

    var2path <- as.character(var2path)
    var2path <- paste(var2path, collapse = "|")
    var2path <- toJSON (var2path)

    ########----------QUERY STRUCTURE AND EXECUTION----------#########
    query <- c('
               {"$and":
               [
               {"$or":
               [ 
               {"Var1": { "$regex": ',var1JSON,' }},
               {"Var1Path": { "$regex": ',var1path,' }}
               ]
               },

               {"$or":
               [
               {"Var2": { "$regex": ',var2JSON,' }},
               {"Var2Path": { "$regex": ',var2path,' }}
               ]
               }
               ]
               }
               ')

    stuff <- loadData()
    stuff
  })

  output$text1 <- renderText(nrow(queryResults()))

1 个答案:

答案 0 :(得分:4)

由于我无法重现您的代码,因此我需要设置自己的数据,但希望这会向您展示mongolite&amp; shiny

数据

将一些数据输入mongodb

library(mongolite)

## create dummy data
df <- data.frame(id = c(1,2,3),
                 region = c("r1", "r1", "r2"))

> df
id region
1  1     r1
2  2     r1
3  3     r2

## insert into database
mong <- mongo(collection = "test", db = "test", url = "mongodb://localhost")
mong$insert(df)
# Complete! Processed total of 3 rows.
# [1] TRUE

<强>光泽

这是一个响应用户输入的非常基本的应用程序。我试图与你的结构保持一致

<强> server.R

library(shiny)
library(jsonlite)
library(mongolite)

loadData <- function(qry){
  mong <- mongo(collection = "test", db = "test", url = "mongodb://localhost",
                verbose = TRUE)

  df <- mong$find(qry)
  return(df)
}


shinyServer(function(input, output) {

  qryResults <- reactive({

    ## This bit responds to the user selection 
    ## which makes it 'reactive'
    region <- list(region = input$si_region)

    qry <- paste0('{ "region" : "',region , '"}')
    df <- loadData(qry)
    return(df)
  })

  output$qry_results <- renderDataTable({
    qryResults()
  })

  output$text1 <- renderText(nrow(qryResults()))

})

<强> ui.R

library(shiny)

shinyUI(navbarPage("mongo query",
                   sidebarLayout(
                     sidebarPanel(
                       selectInput(inputId = "si_region", label = "Select region", choices = c("r1", "r2"))
                     ),
                     mainPanel(
                       verbatimTextOutput(outputId = "text1"),
                       dataTableOutput(outputId = "qry_results")
                     )
                   )))