简体   繁体   English

R的宣传页:标头中定义的CSS在leafletProxy中不起作用

[英]Leaflet for R: CSS defined in header is not working inside leafletProxy

I'm using leaflet and shiny to create a small map with clusters. 我正在使用传单和发亮的方法来创建带有群集的小地图。 For that, I need to change the colours of the clusters, which I've done using code from a reply to this question . 为此,我需要更改群集的颜色,这是我使用对这个问题的答复中的代码完成的。 However, I cannot get it to work within leafletProxy() , which I need in order to allow the user to filter the data being displayed. 但是,我无法使其在leafletProxy()工作,以便允许用户过滤所显示的数据。 Here is an example that shows the problem (I've just coloured all the clusters red): 这是一个显示问题的示例(我将所有群集都涂成了红色):

library(shiny)
library(leaflet)

ui <- fluidPage(
    tags$head(tags$style(HTML("
                              .marker-custom-small {
                              background-color: rgba(255, 0, 0, 1);
                              }
                              .marker-customr-small div {
                              background-color: rgba(255, 0, 0, 1);
                              }

                              .marker-custom-medium {
                              background-color: rgba(255, 0, 0, 1);
                              }
                              .marker-custom-medium div {
                              background-color: rgba(255, 0, 0, 1);
                              }

                              .marker-custom-large {
                              background-color: rgba(255, 0, 0, 1);
                              }
                              .marker-custom-large div {
                              background-color: rgba(255, 0, 0, 1);
                              }"))),
    actionButton("but", "New clusters, please!"),

    leafletOutput("mymap")


  )

  server<-function(input, output, session) {
    output$mymap <- renderLeaflet({
      leaflet(quakes) %>% addTiles() %>% addMarkers(
        clusterOptions = markerClusterOptions(iconCreateFunction=JS("function (cluster) {    
                                                                    var childCount = cluster.getChildCount(); 
                                                                    var c = ' marker-custom-';  
                                                                    if (childCount < 100) {  
                                                                    c += 'large';  
                                                                    } else if (childCount < 1000) {  
                                                                    c += 'medium';  
                                                                    } else { 
                                                                    c += 'small';  
                                                                    }    
                                                                    return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });

    }"))
    )
  })


    observeEvent(input$but, {

      leafletProxy("mymap") %>%

      clearMarkerClusters() %>%

        addMarkers(data = quakes,
          clusterOptions = markerClusterOptions(iconCreateFunction=JS("function (cluster) {    
                                                                      var childCount = cluster.getChildCount(); 
                                                                      var c = ' marker-custom-';  
                                                                      if (childCount < 100) {  
                                                                      c += 'large';  
                                                                      } else if (childCount < 1000) {  
                                                                      c += 'medium';  
                                                                      } else { 
                                                                      c += 'small';  
                                                                      }    
                                                                      return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });

                }"))
        )




    })


  }

  shinyApp(ui,server)

When the user clicks the button, new clusters (of the same points, in this example) should be displayed, but nothing happens. 当用户单击按钮时,应该显示新的群集(在此示例中,这些群集具有相同的点),但是什么也没有发生。 R reports no errors, and the quakes data is being read by addMarkers() , because I get the message Assuming 'long' and 'lat' are longitude and latitude, respectively in the console. R没有报告任何错误,并且通过addMarkers()读取了地震数据,因为我收到消息, Assuming 'long' and 'lat' are longitude and latitude, respectively在控制台中Assuming 'long' and 'lat' are longitude and latitude, respectively I suspect the problem has to do with the addMarkers() call inside leafletProxy() not finding(?) the cluster classes, because replacing... 我怀疑问题已与做addMarkers()内调用leafletProxy()没有找到(?)的簇类,因为更换...

clusterOptions = markerClusterOptions(iconCreateFunction=JS("function (cluster) {    
                                                            var childCount = cluster.getChildCount(); 
                                                            var c = ' marker-custom-';  
                                                            if (childCount < 100) {  
                                                            c += 'large';  
                                                            } else if (childCount < 1000) {  
                                                            c += 'medium';  
                                                            } else { 
                                                            c += 'small';  
                                                            }    
                                                            return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });

                                                            }"))

with just... 只是...

clusterOptions = markerClusterOptions()

...makes the clusters - with the standard colours - display just fine. ...用标准颜色使群集显示得很好。

I would greatly appreciate some help on this! 在此我将不胜感激!

Best, 最好,

Bertel 贝特尔

Following the advice on the Shiny articles site here I managed to get this to work using a custom stylesheet linked to in ui.R with the includeCSS() function. 遵循此处的Shiny ui.R网站上的建议,我设法使用了一个包含在ui.R并通过includeCSS()函数链接到的自定义样式表来ui.R工作。 I haven't used any JS expressions when adding clusters and my custom colour(s) are coming through just fine. 添加簇时我没有使用任何JS表达式,我的自定义颜色也很好。

For example, the code: 例如,代码:

library(shiny)
library(leaflet)

ui <- fluidPage(
    includeCSS("./style.css"),

    actionButton("but", "New clusters, please!"),

  leafletOutput("mymap")


  )

server<-function(input, output, session) {
  output$mymap <- renderLeaflet({
    leaflet(quakes) %>% addTiles() %>% addMarkers(clusterOptions =
                                                    markerClusterOptions())
})


  observeEvent(input$but, {

    leafletProxy("mymap") %>%

      clearMarkerClusters() %>%

      addMarkers(data = quakes,
                 clusterOptions = markerClusterOptions())
  })


}

shinyApp(ui,server)

Should work, you just need to make sure you fully specify all styles that are defined in the default styling (found in ./path/to/your/package/library/leaflet/htmlwidgets/plugins/Leaflet.markercluster/MarkerCluster.Default ) 应该可以使用,您只需要确保完全指定默认样式中定义的所有样式即可(位于./path/to/your/package/library/leaflet/htmlwidgets/plugins/Leaflet.markercluster/MarkerCluster.Default

For example, to colour all clusters red (as per your original example), you can use: 例如,要将所有群集涂成红色(按照您的原始示例),可以使用:

style.css : style.css

.marker-cluster-small {
    background-color: rgba(255, 0, 0, 1);
    }
.marker-cluster-small div {
    background-color: rgba(255, 0, 0, 1);
    }

.marker-cluster-medium {
    background-color: rgba(255, 0, 0, 1);
    }
.marker-cluster-medium div {
    background-color: rgba(255, 0, 0, 1);
    }

.marker-cluster-large {
    background-color: rgba(255, 0, 0, 1);
    }
.marker-cluster-large div {
    background-color: rgba(255, 0, 0, 1);
    }

    /* IE 6-8 fallback colors */
.leaflet-oldie .marker-cluster-small {
    background-color: rgb(255, 0, 0);
    }
.leaflet-oldie .marker-cluster-small div {
    background-color: rgb(255, 0, 0);
    }

.leaflet-oldie .marker-cluster-medium {
    background-color: rgb(255, 0, 0);
    }
.leaflet-oldie .marker-cluster-medium div {
    background-color: rgb(255, 0, 0);
    }

.leaflet-oldie .marker-cluster-large {
    background-color: rgb(255, 0, 0);
    }
.leaflet-oldie .marker-cluster-large div {
    background-color: rgb(255, 0, 0);
}

.marker-cluster {
    background-clip: padding-box;
    border-radius: 20px;
    }
.marker-cluster div {
    width: 30px;
    height: 30px;
    margin-left: 5px;
    margin-top: 5px;

    text-align: center;
    border-radius: 15px;
    font: 12px "Helvetica Neue", Arial, Helvetica, sans-serif;
    }
.marker-cluster span {
    line-height: 30px;
    }

This should allow you to fully customise the clusters, and have them be correctly displayed when you use leafletProxy() . 这应该允许您完全自定义集群, 在使用leafletProxy()时正确显示它们。

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

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