简体   繁体   English

传单代理在闪亮的仪表板中不起作用

[英]leafletProxy not working in shinyDashboard

leafletProxy doesn't seem to be functional within shinyDashboard. LeafletProxy 似乎在 shinyDashboard 中不起作用。 See working example below where choosing different letters should change the circle color.请参阅下面的工作示例,其中选择不同的字母应该会改变圆圈颜色。 Any insight appreciated.任何见解表示赞赏。 Github issue created here: https://github.com/rstudio/shinydashboard/issues/377 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)

There are a few issues regarding your code:您的代码存在一些问题:

1. 1.

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM