闪亮的r子集因子输入

时间:2016-08-16 21:29:40

标签: r shiny shiny-server

STORENUMBER过滤数据并呈现下面的地图和表格,但DMA没有。子集()在因子上的作用是否与server.r中的整数不同?

数据

STORENUMBER = c(123,456)
DMA = c("LA","SD")
LATITUDE = c(130, 132)
LONGITUDE = c(30,35)
locations = data.frame(STORENUMBER, DMA, LATITUDE, LONGITUDE)

ui.r:

       tabItem(tabName = "control",
            fluidPage(
              titlePanel("Control Center"),

              fluidRow(
                  # the Stores are integers
                  column(6,
                      helpText("Test Stores"),                                  
                        # test stores
                        selectInput("testStores", 
                                    label ="Test Stores",
                                    choices = as.vector(unique(locations$STORENUMBER)),
                                    selected = NULL,
                                    multiple = TRUE)
                      ), 
                  # the DMAs are factors
                  column(6,
                      helpText("Test DMA"),
                      selectInput("tDMA", 
                                  label ="Test DMAs",
                                  choices = as.vector(unique(locations$DMA)),
                                  selected = NULL,
                                  multiple = TRUE)
                      ) #column
                  ), #fluidRow


              fluidRow(
                titlePanel("Map"),
                leafletOutput("map"),
                p(),
                actionButton("recalc", "New points")
                ) , 


              fluidRow(
                titlePanel("Test Store Table"),
                column(12,
                       DT::dataTableOutput("tableteststores")
                )  
              )

              ) #fluidPage
            )

这是显示subset()函数的server.r脚本。

server.r:

shinyServer(function(input, output){
  # not sure why DMA isn't working 
  tstores <- reactive({
     subset(locations, DMA %in% input$tDMA | STORENUMBER %in% input$testStores)
    })


  # table of locations
  output$tableteststores <- DT::renderDataTable(DT::datatable(
    data <- as.data.frame(tstores())
  ))  

  # map
  output$map <- renderLeaflet({
    leaflet() %>%
      addProviderTiles("Stamen.TonerLite",
                       options = providerTileOptions(nonWrap = TRUE)
                       ) %>%
      addMarkers(data = tstores())
  })
})

1 个答案:

答案 0 :(得分:0)

使用reactive()函数中的SQL语句查询数据。当在SQL语句的WHERE子句中传递因子作为“输入”变量时,R传递双引号内的因子向量,如(“this”,“that”,“then”)但是要执行SQL,需要使用因子来在WHERE子句中使用单引号传递,如('this','that','then')。如果计划在reactive()函数中使用SQL,请考虑编写这样的输入变量,用双引号替换双引号。

library(RODBC)    

myconn <- odbcConnect('server', uid="user", pwd="password")   

reactive({
  data <- as.data.frame(sqlQuery(myconn, 
        paste(
             "SELECT
                  STORENUMBER
                  ,DMA
                  ,LATITUDE
                  ,LONGITUDE
             FROM database.datatable
             WHERE DMA in", 
                          #this is a way to replace double quotes as single quotes# 
                          #when passing a list or vector of factors# 
                          cat("('",paste(input$DMA, collapse="','"), "')"), "
             OR STORENUMBER in", 
                          # the issue doesn't appear when passing integer types#
                          input$STORENUMBER)  
})

虽然问题中没有显示,但这似乎是我的代码的问题。正如上面的评论所解释的那样,问题中的代码运行正常。只有当尝试在reactive()函数中执行SQL时代码才会失败。在这个答案中解释了原因,这里显示了一个解决方案。希望这会有所帮助。