2016-08-16 163 views
0

STORENUMBER過濾數據並呈現下面的地圖和表格,但DMA沒有。 subset()的工作方式與server.r中的整數不同嗎?閃亮的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 
      ) 

這裏是表示該子集()函數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()) 
    }) 
}) 
+0

你還沒有提供任何測試數據,所以這個問題不是[reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。你究竟是什麼意思,它「不渲染」?你有錯誤嗎?您的選擇是顯示您期望的DMA值還是顯示您的數字? – MrFlick

+0

增加了測試數據,現在可以重現。我可以在表單字段中看到輸入對象,但是當選擇DMA時,映射對象和tableteststores對象不會返回數據。當選擇STORENUMBER時,map和tableteststores會返回對象。所以看起來subset()函數對整數STORENUMBER起作用,但對於因子DMA不起作用。打開你的想法。謝謝。 – JL82559

+0

我無法用您提供的代碼複製任何錯誤。通過DMA過濾似乎工作得很好。 – MrFlick

回答

0

的數據查詢與所述反應性()函數的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時。原因在這個答案中解釋,並在這裏顯示一個解決方案。希望這可以幫助。