簡體   English   中英

傳單代理在閃亮的儀表板中不起作用

[英]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)

您的代碼存在一些問題:

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