我正在制作墨西哥瓦哈卡街头美食地图,尽管遇到了一些麻烦。我正在尝试在 Shiny 中制作传单地图并了解 Shiny 的 UI 端,尽管服务器端没有那么多。我知道我必须将observe()或reactive()添加到服务器端才能产生所需的结果,但我似乎无法让它工作。我将 addlayercontrol() 添加到传单中,因为需要这种效果(根据所选内容隐藏和显示标记),尽管我希望 selectizeImput 来控制图层。下面是我的代码,我包含了数据集的一小部分,以及我在服务器端尝试的一些注释掉的代码。
library(leaflet)
library(shiny)
library(shinydashboard)
library(dplyr)
#Data Sample
long <- c(-96.72363, -96.72880, -96.72700)
lat <- c(17.06167, 17.06200, 17.06170 )
name <- c("jim", "grant", "pablo")
food <- c("tacos", "burger", "elote")
df <- data.frame(long, lat, name, food)
#subsets
palette<- colorFactor(palette = c("blue", "green", "black"),
domain = df$food)
tacos <- dplyr::filter(df, grepl("tacos", food))
burger <- dplyr::filter(df, grepl("burger", food))
elote <- dplyr::filter(df, grepl("elote", food))
#ui
ui <-fluidPage(
titlePanel(title = "Street Food Oaxaca"),
sidebarLayout(
sidebarPanel(
selectizeInput("food","Select a Type:",
choices = unique(df$food),
selected = ,
multiple = TRUE)),
mainPanel(h3("Map"), leafletOutput("m", width = "800", height = "600"))
)
)
#server
server <- function(input, output, session){
output$m = renderLeaflet({
leaflet(data = df) %>%
setView(lng = -96.725, lat = 17.0618, zoom =14)%>%
addTiles() %>%
addCircleMarkers( layerId = tacos,
data = tacos,
~long,
~lat,
group = 'tacos',
popup = ~as.character(name),
radius = 2,
color = ~palette(food),
fillOpacity = 0.5) %>%
addCircleMarkers( layerId = burger,
data = burger,
~long,
~lat,
group = 'burger',
popup = ~as.character(name),
radius = 2,
color = ~palette(food),
fillOpacity = 0.5) %>%
addCircleMarkers( layerId = elote,
data = elote,
~long,
~lat,
group = 'elote',
popup = ~as.character(name),
radius = 2,
color = ~palette(food),
fillOpacity = 0.5) %>%
# addPolygons(data = poly,
# ~long,
# ~lat,
# weight = 3,
# color = "red",
# group = "Restricted Zone" ,
# popup = "Restricted Zone")
addLayersControl(
overlayGroups = c("tacos","burger","elote", "Restricted Zone"),
options = layersControlOptions(collapsed = TRUE)
)
})
}
# observeEvent(
# if(function(input$food, value ="tacos"))
# output$m = renderLeaflet('m')%>%
# removeMarker(layerId = m,c('burger', 'elote')))
#removeMarker(group == C('two', 'three'))
# data_f <- reactive({
# dplyr::filter(df$food == "tacos")
# })
#
# observeEvent(data_f, {
#
# leafletProxy("map") %>%
#
# clearMarkers() })
#Run the application
shinyApp(ui = ui , server = server)
试试这个
library(leaflet)
library(shiny)
library(shinydashboard)
library(dplyr)
#Data Sample
long <- c(-96.72363, -96.72880, -96.72700)
lat <- c(17.06167, 17.06200, 17.06170 )
name <- c("jim", "grant", "pablo")
food <- c("tacos", "burger", "elote")
df <- data.frame(long, lat, name, food)
#subsets
palette <- colorFactor(palette = c("blue", "green", "black"),
domain = df$food)
tacos <- dplyr::filter(df, grepl("tacos", food))
burger <- dplyr::filter(df, grepl("burger", food))
elote <- dplyr::filter(df, grepl("elote", food))
ui <-fluidPage(
titlePanel(title = "Street Food Oaxaca"),
sidebarLayout(
sidebarPanel(
selectizeInput("food","Select a Type:",
choices = unique(df$food),
selected = food[1],
multiple = TRUE)),
mainPanel(h3("Map"), leafletOutput("m", width = "800", height = "600"))
)
)
#server
server <- function(input, output, session){
df1 <- eventReactive(input$food, {
df %>% dplyr::filter(food %in% input$food)
})
output$m = renderLeaflet({
leaflet(data = df1()) %>%
setView(lng = -96.725, lat = 17.0618, zoom =14)%>%
addTiles() %>%
addCircleMarkers( # layerId = input$food,
# data = input$food,
~long,
~lat,
#group = input$food,
#popup = ~as.character(name),
radius = 2,
color = ~palette(input$food),
fillOpacity = 0.5) %>%
# addCircleMarkers( layerId = burger,
# data = burger,
# ~long,
# ~lat,
# group = 'burger',
# popup = ~as.character(name),
# radius = 2,
# color = ~palette(food),
# fillOpacity = 0.5) %>%
# addCircleMarkers( layerId = elote,
# data = elote,
# ~long,
# ~lat,
# group = 'elote',
# popup = ~as.character(name),
# radius = 2,
# color = ~palette(food),
# fillOpacity = 0.5) %>%
# addPolygons(data = poly,
# ~long,
# ~lat,
# weight = 3,
# color = "red",
# group = "Restricted Zone" ,
# popup = "Restricted Zone")
addLayersControl(
overlayGroups = c("tacos","burger","elote", "Restricted Zone"),
options = layersControlOptions(collapsed = TRUE)
)
})
}
#Run the application
shinyApp(ui = ui , server = server)