带有传单的闪亮选择范围和数值变量(作为输入) [英] Shiny selecting ranges and numeric variable (as inputs) with leaflet
问题描述
关于闪亮,我想显示一个数字变量列表和一个滑动条,以便用户可以选择一个数字变量和一个范围.然后,低于该数字的观测值将显示为绿色,而该范围之间的观测值将显示为橙色,而高于该范围的观测值将显示为红色.
On shiny, I would like to show a list of numerical variables and a slide bar so that a user could choose a numerical variable and a range. Then, observations below that number would show up as green, and observations between that range would be orange, and observations above that range would be red.
在我将它们放入闪亮的代码之前,下面的代码可以正常工作.但是我的闪亮代码无法正常工作,所有观察结果均为红色.
The codes below work fine before I put them into shiny. But my shiny codes don't work and all observations are red.
library(Rcpp)
library(ggmap)
library(htmlwidgets)
library(leaflet)
crime2 <- crime[1:50,]
getColor <- function(crime2) {
sapply(crime2$hour, function(hour) {
if(hour< 1) {
"green"
} else if(hour <= 1) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(crime2)
)
leaflet(crime2) %>%
addTiles() %>%
addAwesomeMarkers(~lon, ~lat, icon=icons)
这是无效的代码
ui <- fluidPage(
titlePanel("Unusual Observations"),
sidebarLayout(
sidebarPanel(
helpText("Create maps with
information from the Crime Data"),
selectInput("var",
label = "Choose a variable to display",
choices = c("Hour",
"Number"),
selected = "Hour"),
sliderInput("range",
label = "Range of interest:",
min = 0, max = 10, value = c(1, 2))
),
mainPanel(leafletOutput("map"))
)
)
server <- function(input, output) {
output$map <- renderLeaflet({
data <- switch(input$var,
"hour" = crime2$hour,
"number" = crime2$number)
getColor <- function(data){sapply(data, function(var){
if(input$var< input$range[1]) {
"green"
} else if(input$var <= input$range[2]) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(crime2)
)
leaflet(crime2) %>%
addTiles() %>%
addAwesomeMarkers(~lon, ~lat, icon=icons)
})
}
shinyApp(ui=ui, server=server)
有人知道如何解决所有显示为红色的点"的问题吗?
Does anyone know how to fix 'all points showing up as red' problem?
先谢谢您!
推荐答案
一些更改:
-
创建
selectInput
时,选择(和选择的)选项必须小写以匹配Crime2中的列名.
When you create the
selectInput
the choices (and selected) need to be lowercase to match the column names in crime2.
selectInput("var",
label = "Choose a variable to display",
choices = c("hour",
"number"),
selected = "hour"),
在您的 getColor
函数内部,您希望循环数据中的值,而不是输入$ var,因此您应该调用 var
而不是在lambda函数中输入$ var
.
Inside of your getColor
function you want to loop over the values in data, not input$var, so you should call var
rather than input$var
inside the lambda function.
getColor <- function(data){sapply(data, function(var){
if(var< input$range[1]) {
"green"
} else if(var <= input$range[2]) {
"orange"
} else {
"red"
} })
}
当您实际创建图标时,您希望基于 data
中的值而不是整个Crime2数据集中的值来创建图标.
When you actually go to create your icons, you want them to be created based on the values in data
, not the values in the entire crime2 dataset.
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(data)
)
将它们放在一起:
ui <- fluidPage(
titlePanel("Unusual Observations"),
sidebarLayout(
sidebarPanel(
helpText("Create maps with
information from the Crime Data"),
selectInput("var",
label = "Choose a variable to display",
choices = c("hour",
"number"),
selected = "hour"),
sliderInput("range",
label = "Range of interest:",
min = 0, max = 10, value = c(1, 2))
),
mainPanel(leafletOutput("map"))
)
)
server <- function(input, output) {
output$map <- renderLeaflet({
data <- switch(input$var,
"hour" = crime2$hour,
"number" = crime2$number)
getColor <- function(data){sapply(data, function(var){
if(var< input$range[1]) {
"green"
} else if(var <= input$range[2]) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(data)
)
leaflet(crime2) %>%
addTiles() %>%
addAwesomeMarkers(~lon, ~lat, icon=icons)
})
}
shinyApp(ui=ui, server=server)
这篇关于带有传单的闪亮选择范围和数值变量(作为输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!