我正在使用 Shiny 和 Leaflet 在 R 中编写交互式地图。我编写了 1 个选择所有月份按钮(checkGroup)并且工作正常,但是为其他输入添加选择所有按钮导致所有地图都无法正常工作。
#import data
data <- structure(list(Area = c("Scarborough", "Etobicoke", "East York",
"North York", "North York", "Etobicoke", "Downtown Core (Central)",
"York", "Downtown Core (Central)", "York"), occurrenceyear = c(2017L,
2018L, 2018L, 2018L, 2018L, 2018L, 2017L, 2018L, 2018L, 2018L
), occurrencemonth = structure(c(12L, 5L, 5L, 5L, 5L, 5L, 6L,
12L, 12L, 12L), .Label = c("", "April", "August", "December",
"February", "January", "July", "June", "March", "May", "November",
"October", "September"), class = "factor"), Long = c(-79.1886063,
-79.5458221, -79.3138199, -79.4392548, -79.4406738, -79.5390091,
-79.3820572, -79.4840012, -79.3930817, -79.4356079), Lat = c(43.7639694,
43.5895691, 43.6753197, 43.7586555, 43.727829, 43.6431503, 43.6683502,
43.6842308, 43.6707535, 43.6820869)), row.names = c(NA, 10L), class = "data.frame")
# Define UI ----
ui <- fluidPage(
titlePanel("Interactive Toronto Auto Theft Visualization"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("checkGroup", h3("Month"), choices = list("January", "February", "March", "April", "May", "June", "July", "August" ,"September", "October", "November", "December"), selected = "Janurary"),
actionLink("selectall", "Select All"),
checkboxGroupInput("checkGroup2", h3("Year"),
choices = list(2014, 2015,2016 , 2017, 2018 ), selected = 2018),
actionLink("Selectall2", "Select All"),
checkboxGroupInput("checkGroup3", "Toronto Neighbourhoods", choices = list("Downtown Core (Central)", "East End", "North End", "West End", "East York", "Etobicoke", "North York", "Scarborough", "York"), selected = "York"),
actionLink("Selectall3", "Select All")
),
mainPanel (leafletOutput("map", "100%", 500))
))
# Define server logic ----
server <- function(input, output, session){
observe({
if(input$selectall == 0) return(NULL)
else if(input$selectall%%2==0)
{
updateCheckboxGroupInput(session, "checkGroup", "Month", choices = list("January", "February", "March", "April", "May", "June", "July", "August" ,"September", "October", "November", "December"))
}
else
{
updateCheckboxGroupInput(session, "checkGroup", "Month", choices = list("January", "February", "March", "April", "May", "June", "July", "August" ,"September", "October", "November", "December"), selected = list("January", "February", "March", "April", "May", "June", "July", "August" ,"September", "October", "November", "December"))
}
if(input$Selectall2 == 0) return(NULL)
else if(input$Selectall2 %%2 == 0)
{
updateCheckboxGroupInput(session, "checkGroup2", "Year", choices = list(2014, 2015,2016 , 2017, 2018))
}
else
{
updateCheckboxGroupInput(session, "checkGroup2", "Year", choices = list(2014, 2015,2016 , 2017, 2018), selected = list(2014, 2015,2016 , 2017, 2018))
}
})
filtered <- reactive({
if (is.null(input$checkGroup) & is.null(input$checkGroup2) & is.null(input$checkGroup3)){
return (NULL)
}
data %>% filter(occurrencemonth %in% input$checkGroup & occurrenceyear %in% input$checkGroup2 & Area %in% input$checkGroup3)
})
output$map <- renderLeaflet({
leaflet()%>%
addProviderTiles("CartoDB") %>%
addCircleMarkers(data = filtered(), radius = 2)
})
}
我认为问题出在观察函数中,因为这是选择所有按钮的编程所在。在遇到问题之前,我只对其中的 2 个按钮进行了编程,并在添加第三个按钮 (selectall3) 之前尝试解决问题。
我尝试为两个单独的按钮创建两个单独的观察功能,但这并没有解决问题。
您这里有错字:
if(input$selectall2 == 0) return(NULL)
应该是:
if(input$Selectall2 == 0) return(NULL)