leafletProxy 在 shinyDashboard 中不工作
leafletProxy not working in shinyDashboard
leafletProxy 在 shinyDashboard 中似乎不起作用。请参阅下面的工作示例,其中选择不同的字母会改变圆圈的颜色。任何见解表示赞赏。 Github 问题在此处创建:https://github.com/rstudio/shinydashboard/issues/377
library(shiny)
library(shinydashboard)
library(leaflet)
library(sf)
n = 100
df1 = data.frame(id = 1:n,
x = rnorm(n, 10, 3),
y = rnorm(n, 49, 1.8))
pts = st_as_sf(df1, coords = c("x", "y"), crs = 4326)
map <- leaflet() %>%
addProviderTiles(provider = providers$CartoDB.DarkMatter) %>%
addCircles(data = pts, group = "pts") %>%
setView(lng = 10.5, lat = 49.5, zoom = 6)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput(inputId = 'click', 'Choose one:', c('A', 'B', 'C'))
),
dashboardBody(
fluidRow(
div(
id = "map",
column(
width = 12,
leafletOutput('map', height = '800px')),
)
)
)
)
server <- function(input, output) {
output$map <- renderLeaflet({map})
observeEvent(input$click, {
col <- switch(input$click,
'A' = 'green',
'B' = 'yellow',
'C' = 'white')
m <- leafletProxy("map") %>%
clearShapes() %>%
addCircles(data = pts,
color = col)
m
})
}
shinyApp(ui, server)
关于您的代码有两个问题:
leafletProxy
需要一个会话参数(将其添加到您的服务器函数)
您将传单地图放置在与您的地图相同 id
的 div
中。 - ID 必须是唯一的。
library(shiny)
library(shinydashboard)
library(leaflet)
library(sf)
n = 100
df1 = data.frame(id = 1:n,
x = rnorm(n, 10, 3),
y = rnorm(n, 49, 1.8))
pts = st_as_sf(df1, coords = c("x", "y"), crs = 4326)
map <- leaflet() %>%
addProviderTiles(provider = providers$CartoDB.DarkMatter) %>%
addCircles(data = pts, group = "pts") %>%
setView(lng = 10.5, lat = 49.5, zoom = 6)
ui <- dashboardPage(dashboardHeader(),
dashboardSidebar(selectInput(
inputId = 'color', 'Choose one:', list(A = 'green',
B = 'yellow',
C = 'white')
)),
dashboardBody(fluidRow(column(
width = 12,
leafletOutput('map', height = '800px')
))))
server <- function(input, output, session) {
output$map <- renderLeaflet({
map
})
m <- leafletProxy("map", session)
observeEvent(input$color, {
m %>% clearShapes() %>%
addCircles(data = pts,
color = input$color)
}, ignoreInit = TRUE)
}
shinyApp(ui, server)
leafletProxy 在 shinyDashboard 中似乎不起作用。请参阅下面的工作示例,其中选择不同的字母会改变圆圈的颜色。任何见解表示赞赏。 Github 问题在此处创建:https://github.com/rstudio/shinydashboard/issues/377
library(shiny)
library(shinydashboard)
library(leaflet)
library(sf)
n = 100
df1 = data.frame(id = 1:n,
x = rnorm(n, 10, 3),
y = rnorm(n, 49, 1.8))
pts = st_as_sf(df1, coords = c("x", "y"), crs = 4326)
map <- leaflet() %>%
addProviderTiles(provider = providers$CartoDB.DarkMatter) %>%
addCircles(data = pts, group = "pts") %>%
setView(lng = 10.5, lat = 49.5, zoom = 6)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput(inputId = 'click', 'Choose one:', c('A', 'B', 'C'))
),
dashboardBody(
fluidRow(
div(
id = "map",
column(
width = 12,
leafletOutput('map', height = '800px')),
)
)
)
)
server <- function(input, output) {
output$map <- renderLeaflet({map})
observeEvent(input$click, {
col <- switch(input$click,
'A' = 'green',
'B' = 'yellow',
'C' = 'white')
m <- leafletProxy("map") %>%
clearShapes() %>%
addCircles(data = pts,
color = col)
m
})
}
shinyApp(ui, server)
关于您的代码有两个问题:
leafletProxy
需要一个会话参数(将其添加到您的服务器函数)您将传单地图放置在与您的地图相同
id
的div
中。 - ID 必须是唯一的。
library(shiny)
library(shinydashboard)
library(leaflet)
library(sf)
n = 100
df1 = data.frame(id = 1:n,
x = rnorm(n, 10, 3),
y = rnorm(n, 49, 1.8))
pts = st_as_sf(df1, coords = c("x", "y"), crs = 4326)
map <- leaflet() %>%
addProviderTiles(provider = providers$CartoDB.DarkMatter) %>%
addCircles(data = pts, group = "pts") %>%
setView(lng = 10.5, lat = 49.5, zoom = 6)
ui <- dashboardPage(dashboardHeader(),
dashboardSidebar(selectInput(
inputId = 'color', 'Choose one:', list(A = 'green',
B = 'yellow',
C = 'white')
)),
dashboardBody(fluidRow(column(
width = 12,
leafletOutput('map', height = '800px')
))))
server <- function(input, output, session) {
output$map <- renderLeaflet({
map
})
m <- leafletProxy("map", session)
observeEvent(input$color, {
m %>% clearShapes() %>%
addCircles(data = pts,
color = input$color)
}, ignoreInit = TRUE)
}
shinyApp(ui, server)