【问题标题】:How to change circle marker attributes (e.g. opacity & fillOpacity) within mapview::addFeatures如何在 mapview::addFeatures 中更改圆形标记属性(例如不透明度和填充不透明度)
【发布时间】:2018-06-07 01:42:03
【问题描述】:

我已经能够在 R-Shiny 应用程序中生成地图,该应用程序允许通过结合 mapview、mapedit 和传单包来多选标记。 功能上一切都很好,除了我希望修改标记属性,更具体地说是增加 fillOpacity。

mapview AddFeatures 的文档建议它应该接受与传单用于 addCircleMarkers 的相同参数。我也尝试使用函数 addCircleMarkers 而不是 addFeatures 没有任何成功。

addFeatures doco

... 进一步的参数传递给相应的传单::add* 职能。请参阅 addCircleMarkers、addPolylines 和 addPolygons。

但它似乎忽略了论点;重量、不透明度和填充不透明度。我在下面的独立代码中标记了哪些参数有效或无效。

是我做错了什么还是你认为这是一个错误?

# devtools::install_github("r-spatial/sf")
# devtools::install_github("r-spatial/mapview@develop")
# devtools::install_github("bhaskarvk/leaflet.extras")
# devtools::install_github("r-spatial/mapedit")
library(tidyverse)
library(sf)
library(leaflet)
library(mapedit)
library(mapview)
library(shiny)
library(shinyjs)

locnCoord <-
  data.frame(location = c('Sit1','Site2','Site3'),
             lat=c(-18.1, -18.3, -18.4),
             lon=c(145.8, 145.9, 145.9)) %>%
  mutate(depth = runif(3))

locnSF <- st_as_sf(locnCoord, coords = c('lon','lat'), crs="+proj=longlat +datum=WGS84 +no_defs")

#### User input

ui <- fluidPage(
  shinyjs::useShinyjs(),
  shinyjs::extendShinyjs(text = "shinyjs.refresh = function() { location.reload(); }"),
  fluidRow(
    # edit module ui
    column(6,
           selectModUI("selectmap"),
           actionButton("refresh", "Refresh Map")
           ),
    column(6,
           h3("Point of Depth"),
           plotOutput("selectstat")
           )
    )
  )

#### Server

server <- function(input, output, session) {

  observeEvent(input$refresh, {
    shinyjs::js$refresh()
  })

  g_sel <- callModule(
    selectMod,
    "selectmap",
    leaflet() %>%
      addTiles() %>%
      addFeatures(
      # addCircleMarkers(
        data = locnSF,
        layerId = ~location,
        stroke = TRUE, # This is effective
        color = 'red', # This is effective
        weight = 150, # This is ignored    ####
        opacity = 0, # This is ignored     ####
        fill = TRUE, # This is effective
        fillColor = 'blue', # This is effective
        fillOpacity=1, # This is ignored   ####
        radius=20) # This is effective
  )

  rv <- reactiveValues(selected=NULL)

  observe({
    gs <- g_sel()

    if(length(gs$id) > 0) {
      rv$selected <- locnSF %>% filter(location %in% gs$id)
    } else {
      rv$selected <- NULL
    }
  })

  output$selectstat <- renderPlot({
    ggplot()
    if(!is.null(rv$selected) && nrow(rv$selected) > 0) {
      ggplot(data=rv$selected, aes(location, depth))+
        geom_point(color='red', size=5)
    } else {
      ggplot()
    }
  })
}
shinyApp(ui, server)

【问题讨论】:

    标签: r shiny leaflet android-mapview r-leaflet


    【解决方案1】:

    它们不会被忽略。如果您尝试作为独立的 mapview 调用,您将看到它们有效。在您的应用程序中,它们被 mapedit 模块 selectMod 屏蔽,该模块允许您分别设置 opacityfillOpacityweight,以便在通过参数 styleTruestyleTrue 选择和未选择功能的情况下,分别。当您设置这些时,您将获得所需的行为。因此,更改您调用 selectMod 模块的部分,如下所示:

    g_sel <- callModule(
        selectMod,
        "selectmap",
        leaflet() %>%
          addTiles() %>%
          addFeatures(
            # addCircleMarkers(
            data = locnSF,
            layerId = ~location,
            stroke = TRUE, # This is effective
            color = 'red', # This is effective
            # weight = 150, # This is ignored    ####
            # opacity = 0, # This is ignored     ####
            fill = TRUE, # This is effective
            fillColor = 'blue', # This is effective
            # fillOpacity=1, # This is ignored   ####
            radius=20), # This is effective
        styleFalse = list(fillOpacity = 0.5, weight = 0, opacity = 0), 
        styleTrue = list(fillOpacity = 1, weight = 5, opacity = 1)
      )
    

    【讨论】:

    • 谢谢蒂姆。现在完美运行。
    猜你喜欢
    • 2016-10-12
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 2013-04-05
    • 2020-08-04
    相关资源
    最近更新 更多