簡體   English   中英

閃亮的r子集因子輸入

[英]shiny r subset factor input

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())
  })
})

在react()函數中使用SQL語句查詢數據。 在SQL語句的WHERE子句中將因子作為“輸入”傳遞變量時,R會在雙引號中傳遞因子的向量,例如(“ this”,“ that”,“ then”),但是要執行SQL,它需要使用在WHERE子句中使用單引號(例如('this','that','then'))傳遞。 如果計划在react()函數中使用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)  
})

盡管未在問題中顯示,但這似乎是我的代碼存在的問題。 正如上面的注釋所解釋的,問題中的代碼可以正常工作。 僅當嘗試在react()函數中執行SQL時,代碼才會失敗。 在此答案中說明了原因,並在此處顯示了解決方案。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM