지도에 호텔을 시각화하는 반짝이는 앱을 만들고 있습니다 (리플릿 패키지 사용). 데이터 하위 집합을로드 할 수있는 드롭 다운 목록이 있습니다. 또한 왼쪽 상단에 필터 버튼을 만들어 연도별로 데이터를 추가로 필터링하는 데 사용할 수 있습니다.
apply
버튼을 클릭했을 때만 필터가 반응하고 싶습니다 . 또한 clear
버튼을 클릭하면 필터를 지우고 싶습니다 .
아래는 내 반짝이는 앱 코드입니다. 내 apply
및 clear
버튼이 작동하지 않는 것 같습니다 . 필터 드롭 다운에서 연도를 선택하면 내지도가 새로 고쳐집니다. apply
버튼을 클릭 한 후에 만 새로 고침하고 싶습니다 . 필터는 또한 '클리어'에 응답하지 않습니다. 나는 몇 시간을 보냈지 만 이유를 알 수 없었다. 내가 뭘 잘못했는지 아는 사람이 있습니까?
이것은 훨씬 단순화 된 버전입니다. 내 실제 앱은 훨씬 더 복잡합니다. 드롭 다운 버튼에 여러 필터가 있습니다.
library(shiny)
library(leaflet)
library(shinyjs)
library(shinyWidgets)
library(dplyr)
df <- read.table(text = "Name Year Value longitude latitude
A 2020 12 -106.50645 35.06745
B 2020 33 -116.00994 36.21834
C 2020 44 -115.24685 36.29460
A 2019 55 -115.24685 35.06745
B 2019 22 -118.18642 33.98881
C 2019 11 -111.83064 35.06745",
header = TRUE)
ui <- fluidPage(
selectizeInput(inputId = 'hotel', label='Hotel Type:',
choices = c('A', 'B', 'C'),
multiple=TRUE,
options = list(
maxItems = 1,
placeholder = '',
onInitialize = I("function() { this.setValue(''); }"))),
actionButton("load", "load"),
div(id = 'map_filter',
dropdown(
tags$h3("Filters"),
selectizeInput(inputId = 'year', label='Choose Year:',
choices = c(2020,2019),
multiple=TRUE,
options = list(
maxItems = 2,
placeholder = '',
onInitialize = I("function() { this.setValue(''); }"))),
#uiOutput('map_zipcode_prod2ui') ,
# column(width=1, offset = 1, actionButton(inputId='map_zipcode_applyProductFilter',
# label='Apply Filter', style = 'margin-top:25px') )
actionBttn(
inputId = 'applyFilter',
label = "Apply",
style = "gradient",
color = "danger",
icon = icon("") ) ,
actionBttn(
inputId = 'clearFilter',
label = "Clear",
style = "gradient",
color = "danger",
icon = icon("") ) ,
style = "unite", icon = icon("filter"),
status = "danger", width = "300px"
) #dropdown
), #div
leafletOutput("mymap")
)
server <- function(input, output, session) {
df1 = eventReactive (input$load, {
df %>% filter(Name == input$hotel)
})
df2 = reactive({
if (length(input$year)==0) {
df2 = df1()
} else {
df2 = df1() %>% filter(Year %in% input$year)
}
})
# clear all filters
observeEvent(input$clearFilter, {
reset("map_filter")
})
observeEvent(c(input$load,input$applyFilter), ignoreInit = T, ignoreNULL = T, {
output$mymap <- renderLeaflet({
map <- leaflet() %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
addCircleMarkers( data = df2(),
#group = 'Data Markers 1',
lng = ~longitude,
lat = ~latitude,
radius = 10,
stroke = F,
fillOpacity = 0.9,
color = 'red')
})
})
}
shinyApp(ui, server)
여기에 한 가지 접근 방식이 있습니다. 이것이 도움이되기를 바랍니다.
지도를 reactiveVal
. 그런 다음로드하거나 필터 버튼을 적용하여 맵을 업데이트합니다. 그렇지 않으면지도가 변경되지 않습니다.
updateSelectizeInput
필터를 지우는 데 사용 하는 것이 좋습니다.
편집 : 주석을 기반으로 df1
하며 df2
결합되지 않습니다.
server <- function(input, output, session) {
map <- reactiveVal(NULL)
df1 = reactive({
req(input$hotel)
filter(df, Name == input$hotel)
})
df2 = reactive({
if (length(input$year)==0) {
df2 = df1()
} else {
df2 = df1() %>% filter(Year %in% input$year)
}
})
# clear all filters
observeEvent(input$clearFilter, {
updateSelectizeInput(session, inputId = 'year', selected = "")
})
output$mymap <- renderLeaflet({
map()
})
observeEvent(c(input$load, input$applyFilter), ignoreInit = T, ignoreNULL = T, {
map(leaflet() %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
addCircleMarkers( data = df2(),
#group = 'Data Markers 1',
lng = ~longitude,
lat = ~latitude,
radius = 10,
stroke = F,
fillOpacity = 0.9,
color = 'red')
)
})
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다