闪亮的 r 子集因子输入
shiny r subset factor input
STORENUMBER 过滤数据并呈现下面的地图和 table,但 DMA 不会。 subset() 在 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())
})
})
在reactive()函数中用SQL语句查询数据。当在 SQL 语句的 WHERE 子句中将因子作为 "input" 变量传递时,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)
})
虽然问题中没有显示,但这似乎是我的代码的问题。正如上面的评论所解释的那样,问题中的代码工作正常。仅当尝试在代码失败的 react() 函数中执行 SQL 时。此答案中解释了原因,此处显示了解决方案。希望这有帮助。
STORENUMBER 过滤数据并呈现下面的地图和 table,但 DMA 不会。 subset() 在 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())
})
})
在reactive()函数中用SQL语句查询数据。当在 SQL 语句的 WHERE 子句中将因子作为 "input" 变量传递时,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)
})
虽然问题中没有显示,但这似乎是我的代码的问题。正如上面的评论所解释的那样,问题中的代码工作正常。仅当尝试在代码失败的 react() 函数中执行 SQL 时。此答案中解释了原因,此处显示了解决方案。希望这有帮助。