使用rCharts在R和Shiny中创建传单热图 [英] Creating Leaflet heatmaps in r and shiny using rCharts

查看:171
本文介绍了使用rCharts在R和Shiny中创建传单热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ramnath Vaidyanathan在 http:上的出色演示//rmaps.github.io/blog/posts/leaflet-heat-maps/index.html ,我想为我闪亮的应用程序重现他的热图.

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

当我尝试闪亮地使用Ramnath的代码时,尽管我只能设法将地图显示出来,而不能将热点地图显示出来. 我出现问题的部分原因可能是Ramnath的原始代码使用了rMaps,而我使用的是rCharts(也由Ramnath开发),因为它更加发达/与光泽度更好地集成了,当然包括Leaflet.我试图将rMaps与Shiny的HTML通用命令renderUIhtmlOutput结合使用,但没有成功.

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(
 "http://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="http://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
      ))))

    })
  }
  ))

并显示它正常工作

这篇关于使用rCharts在R和Shiny中创建传单热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆