简体   繁体   English

闪亮的r子集因子输入

[英]shiny r subset factor input

The STORENUMBER filters the data and renders the map and table below, but the DMA doesn't. STORENUMBER会过滤数据并呈现下面的地图和表格,但DMA不会。 Does subset() work differently on factors than integers in server.r? 子集()在因子上的作用是否不同于server.r中的整数?

data 数据

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 : 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
            )

Here is the server.r script showing the subset() function. 这是显示subset()函数的server.r脚本。

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

The data is queried with a SQL statement in the reactive() function. 在react()函数中使用SQL语句查询数据。 When passing factors as "input" variables in the WHERE clause of the SQL statement R passes a vector of factors within double quotes like ("this", "that", "then") but for the SQL to execute it needs the factors to be passed with single quotes like ('this', 'that', 'then') in the WHERE clause. 在SQL语句的WHERE子句中将因子作为“输入”传递变量时,R会在双引号中传递因子的向量,例如(“ this”,“ that”,“ then”),但是要执行SQL,它需要使用在WHERE子句中使用单引号(例如('this','that','then'))传递。 Consider writing the input variables like this to replace the double quotes with single quotes if planning to use SQL within the reactive() function. 如果计划在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)  
})

Although not shown in the question, this appears to have been the issue with my code. 尽管未在问题中显示,但这似乎是我的代码存在的问题。 As the comments above explain, the code in the question works fine. 正如上面的注释所解释的,问题中的代码可以正常工作。 It's only when trying to do the SQL in a reactive() function that the code fails. 仅当尝试在react()函数中执行SQL时,代码才会失败。 The reason is explained in this answer and a solution is shown here. 在此答案中说明了原因,并在此处显示了解决方案。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM