使用KML为R Shiny/R Studio + rMaps联网 [英] Networking for R Shiny / R Studio + rMaps with KML
问题描述
这是对使用 jdharrison 开发的代码和讨论
Using the code developed by jdharrison and the discussion here, here is a minimal ui.R:
library(shiny);library(rCharts)
shinyUI(fluidPage(
mainPanel(
tabPanel("Interactive", tags$style('.leaflet {height: 1000px;}'),
showOutput('mapPlot', 'leaflet'))
)) )
还有一个最小的服务器.R:
And a minimal server.R:
library(shiny);library(rCharts);library(rMaps)
shinyServer(function(input, output,session) {
output$mapPlot <- renderMap({
map1 = Leaflet$new()
map1$setView(c(45.5236, -122.675), 13)
map1$tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
map1$addAssets(css = NULL, jshead = 'http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js')
map1$addKML('leaflet/placemark.kml')
leafletLib <- file.path(find.package("rMaps"), "libraries", "leaflet")
sampleKml <- readLines('http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml')
write(sampleKml, file.path(leafletLib, 'placemark.kml'))
map1
}) })
当我使用RStudio
在服务器上或在实时网站上运行shiny::runApp()
时,会得到一个空白地图,类似于我在上述解决方案之前在本地遇到的问题.
When I run shiny::runApp()
on my server with RStudio
or on a live Website I get a blank map, similar to the issue I had locally prior to the aforementioned solution.
我确定这与KML文件的位置以及文件权限有关,但是我很难使其与KML文件一起使用.
I'm sure this has something to do with the location of the KML file and perhaps file permissions, but I'm having some difficulty getting it to work -with- the KML file.
感谢您提供的任何提示或资源.
Thank you for any tips or resources you may have.
更新:我在本地尝试过,并得到了相同的结果.因此,我不确定这与服务器网络有关...
UPDATE: I tried it locally and get the same result. So, I'm not sure it has something to do with my server networking...
推荐答案
这里有一些问题.当同时加载rMaps
和rMaps
时,它们将覆盖rMaps
.因此,Leaflet$new
调用实际上来自rCharts
包.同样,不可能使用以前使用的addAssets
方法.有必要更改libraries/leaflet/config.yml
文件并添加一个
leaflet-kml.js
链接.还需要将该文件下载到libraries/leaflet/external/leaflet-kml.js
There are a few issues here. rCharts
overrides rMaps
when they are both loaded up. So the Leaflet$new
call is actually coming from the rCharts
package. Also it is not possible to use the addAssets
method that was utilised previously. It is necessary to change the libraries/leaflet/config.yml
file and add a
leaflet-kml.js
link. It is also necessary to download that file to libraries/leaflet/external/leaflet-kml.js
首先,我们将插件添加到rcharts传单javascript文件中
First we add the plugin to the rcharts leaflet javascript files
require(yaml)
leafletLib <- file.path(find.package("rCharts"), "libraries", "leaflet")
rMapsConfig <- yaml.load_file(file.path(leafletLib, "config.yml"))
# add a kml library
kmlLib <- readLines("http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js")
write(kmlLib, file.path(leafletLib, "external", "leaflet-kml.js"))
# add the library to config.yml
rMapsConfig$leaflet$jshead <- union(rMapsConfig$leaflet$jshead , "external/leaflet-kml.js")
write(as.yaml(rMapsConfig), file.path(leafletLib, "config.yml"))
现在我们可以看看使用闪亮的
Now we can look at using shiny
library(shiny)
library(rCharts)
library(rMaps)
runApp(
list(ui =fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 0, max = 1000, value = 500)
),
mainPanel(
tabsetPanel(
tabPanel("Interactive", tags$style('.leaflet {height: 1000px;}'),
showOutput('mapPlot', 'leaflet'))
)
)
)
),
server = function(input, output,session) {
output$mapPlot <- renderUI({
map1 = Leaflet$new()
map1$setView(c(45.5236, -122.675), 13)
map1$tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
map1$addKML('leaflet/placemark.kml')
leafletLib <- file.path(find.package("rCharts"), "libraries", "leaflet")
sampleKml <- readLines('http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml')
write(sampleKml, file.path(leafletLib, 'placemark.kml'))
HTML(map1$html(chartId = "mapPlot"))})
})
)
这篇关于使用KML为R Shiny/R Studio + rMaps联网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!