我正在嘗試編寫一個應用程序,允許用戶通過checkboxGroupInput選擇多組點,并在傳單地圖上顯示具有相應緯度/經度的相關點。我的dataframe如下:
x3 = structure(list(
species = c("Duck", "Duck", "Goose", "Goose", "Swan", "Swan"),
lat = c(42, 43, 47, 38, 40, 40),
long = c(-110, -88, -122, -77, -112, -109)),
row.names = c(NA, 6L), class = "data.frame")
使用這個dataframe,我預計每個物種會出現兩個點,如果所有三個物種框都被選中,一次最多會出現六個點。然而,我一直無法一次獲得兩個以上的分數來展示。如果一次只選中一個框,則點是正確的,但當選中兩個或多個框時,兩個類別中的每一個都會顯示一個點,而其他點則會丟失。我的代碼如下:
library(shiny)
library(leaflet)
#create dataframe
x3 = structure(list(
species = c("Duck", "Duck", "Goose", "Goose", "Swan", "Swan"),
lat = c(42, 43, 47, 38, 40, 40),
long = c(-110, -88, -122, -77, -112, -109)),
row.names = c(NA, 6L), class = "data.frame")
#setup user interface
ui = fluidPage(
titlePanel("Bird Map"),
sidebarLayout(
sidebarPanel = sidebarPanel(
checkboxGroupInput(inputId = "species1",
label = "Species",
choices = list("Duck",
"Goose",
"Swan"),
selected = "Duck"),
),
#Main panel appearance
mainPanel = mainPanel(
leafletOutput(outputId = 'map')
)
)
)
#Reactive server function
server = function(input, output){
map_x3 = reactive({
x3 %>%
filter(species == input$species1)
})
#Leaflet map output
output$map = renderLeaflet({
leaflet(map_x3()) %>%
addTiles() %>%
setView(lng = -90, lat = 36.2, zoom = 4)%>%
clearShapes() %>%
addMarkers(label = ~ species)
})
}
#Run app
shinyApp(ui = ui, server = server)
提前感謝您,非常感謝您的幫助。
我嘗試了不同的方式來顯示數據,簡化和更復雜的數據集,一個observe()部分,以及額外的reactive表達式。問題仍然存在,盡管在較大的數據集中,我經常能夠同時顯示兩個以上的點(盡管在選中多個框時,所有點都不可見)。
您應該將
x3 %>% filter(species == input$species1)
替換為