基于多个反应式表达式动态更改传单地图 [英] Change Leaflet Map Dynamically based on Multiple Reactive expressions
问题描述
在示例数据框DF
中,我有以下几列.
In the example Data Frame DF
, I have the following columns.
gender <- c("Male", "Female","Female", "Male")
Location <- c("AB", "BC", "CD", "DE")
hasTV <- c("Yes","Yes","No","No")
Latitude <- c(49.82380908513249,59.478568831926395,59.478568831926395,49.82380908513249)
Longitude <- c(-10.8544921875,-10.8544921875,2.021484375,2.021484375)
DF <- data.frame(gender,Location,hasTV,Latitude,Longitude)
在用户界面下,我使用radiobuttons
从hasTV中选择选项,使用checkboxGroupInput
选择性别,并使用selectInput
创建位置下拉列表.我已经在fluidRow
中实现了这一点,如下所示.
Under UI, i've used radiobuttons
to select options from hasTV, checkboxGroupInput
to select Gender and selectInput
to create a dropdown of Location. I've implemented this in fluidRow
as shown below.
sex <- unique(DF$gender)
loc <- unique(DF$Location)
hastv <- unique(DF$hasTV)
radioButtons("radio_hastv",label = "Has TV", choices = hastv, selected = "Yes")
checkboxGroupInput("checkbox_gender", label = "Gender", choices = sex, selected = sex)
selectInput("Location", label = "Location", choices=loc, selected = "AB")
leafletOutput("mymap", height = 415)
在服务器功能中,我基于所选的输入有多个反应式.这就是我实现表达式的方式.
In the server function, I have multiple reactive expressions based on the input selected. This is how I've implemented the expressions.
filtered_gender <- reactive({
DF[DF$gender == input$checkbox_gender,]
})
filtered_hastv <- reactive({
DF[DF$hasTV == input$radio_hastv,]
})
filtered_loc <- reactive({
DF[DF$Location == input$loc,]
})
我已经绘制了传单地图.但是,我希望每当以某种方式选择所有这三个输入时,我的地图就会改变.例如如果有人选择,性别=男性,位置= DE,hasTV =否,则会在地图上绘制具有正确gps的适当地图.
I've already rendered the leaflet map. However, I'd like my map to change whenever all these three inputs are somehow selected. e.g. if a person selects, gender=Male, location = DE and hasTV=No, the appropriate map with the correct gps is plotted on the map.
到目前为止,我只能使用一个反应式来更新传单图,如下所示.
So far, i'm only able to update leaflet map using one reactive expression as shown below.
observe(leafletProxy("mymap", data = filtered_loc()) %>%
clearMarkers()%>%
addMarkers(radius =3)%>%
label = ~htmlEscape(DF$hasTV)
)
我该如何合并其他反应式,以使映射相应地变化.谢谢.
How do I go about incorporating the other reactive expressions so that the map changes accordingly. Thanks.
推荐答案
您需要将所有这些过滤器移动到一个反应堆中,并在传单图中使用它-
You need to move all those filters in one reactive and use that in your leaflet plot -
library(dplyr)
filtered_data <- reactive({
DF %>%
filter(gender %in% input$checkbox_gender,
hasTV %in% input$radio_hastv,
Location %in% input$loc
)
})
observe(leafletProxy("mymap", data = filtered_data()) %>%
clearMarkers()%>%
addMarkers(radius =3)%>%
label = ~htmlEscape(hasTV) # note change; using DF here is wrong
)
这篇关于基于多个反应式表达式动态更改传单地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!