2

我想在 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()))
4

1 回答 1

4

由于我无法重现您的代码,因此我正在设置自己的数据,但希望这将向您展示mongolite&的工作示例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

闪亮的

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

服务器.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()))

})

用户界面

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")
                     )
                   )))
于 2016-02-05T20:24:52.283 回答