闪亮的r子集因子输入

问题描述:

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://*.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

的数据查询与所述反应性()函数的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时。原因在这个答案中解释,并在这里显示一个解决方案。希望这可以帮助。