在 r 中创建 Leaflet 热图并使用 rCharts 闪亮

编程入门 行业动态 更新时间:2024-10-14 00:27:22
本文介绍了在 r 中创建 Leaflet 热图并使用 rCharts 闪亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Ramnath Vaidyanathan 在

I am using the great demo by Ramnath Vaidyanathan at rmaps.github.io/blog/posts/leaflet-heat-maps/index.html and I would like to reproduce his heat map for my shiny application.

When I try to use Ramnath's code in shiny though I only manage to get the map out, but not the heat map. Possibly part of the reason of my problems is that the original code from Ramnath uses rMaps while I'm using rCharts (also developed by Ramnath) as it is more developed / better integrated with shiny and of course includes Leaflet. I tried to use rMaps with shiny's HTML generic commands renderUI and htmlOutput with no success.

This is the shiny code that doesn't work (it just displays the map ignoring the hotspot library):

library(rCharts) library(shiny) runApp( list(ui = (pageWithSidebar( headerPanel("Heatmap"), sidebarPanel( width=2), mainPanel( mapOutput("leafmap") ) )), server = function(input, output) { output$leafmap <- renderMap({ L2 <- Leaflet$new() L2$setView(c(29.7632836, -95.3632715), 10) L2$tileLayer(provider = "MapQuestOpen.OSM") data(crime, package = 'ggmap') library(plyr) crime_dat = ddply(crime, .(lat, lon), summarise, count = length(address)) crime_dat = toJSONArray2(na.omit(crime_dat), json = F, names = F) L2$addAssets(jshead = c( "leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js" )) L2$setTemplate(afterScript = sprintf(" <script> var addressPoints = %s var heat = L.heatLayer(addressPoints).addTo(map) </script> ", rjson::toJSON(crime_dat) )) L2 }) } ))

解决方案

Turning my comment into an answer (making use of this question/answer)

library(rCharts) library(shiny) library(data.table) runApp( list(ui = (pageWithSidebar( headerPanel("Heatmap"), sidebarPanel( width=2), mainPanel( chartOutput("baseMap", "leaflet"), tags$style('.leaflet {height: 500px;}'), tags$head(tags$script(src="leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js")), uiOutput('heatMap') ) )), server = function(input, output) { data(crime, package="ggmap") crime <- as.data.table(crime) output$baseMap <- renderMap({ baseMap <- Leaflet$new() baseMap$setView(c(29.7632836, -95.3632715), 10) baseMap$tileLayer(provider = "MapQuestOpen.OSM") baseMap }) output$heatMap <- renderUI({ ## changed to use data.table for speed crime_dat <- crime[(lat != ""), .(count = .N), by=.(lat, lon)] ## there's a blank in there somewhere ## I was having issues with toJSON, so I'm creating my own JSON j <- paste0("[",crime_dat[,lat], ",", crime_dat[,lon], ",", crime_dat[,count], "]", collapse=",") j <- paste0("[",j,"]") tags$body(tags$script(HTML(sprintf(" var addressPoints = %s var heat = L.heatLayer(addressPoints).addTo(map)" , j )))) }) } ))

And to show it working

更多推荐

在 r 中创建 Leaflet 热图并使用 rCharts 闪亮

本文发布于:2023-07-18 14:13:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:热图   Leaflet   rCharts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!