1

我正在关注这个例子:

https://gist.github.com/ramnathv/9998388

我的目标是绘制一条路线。使用 RStudio 它工作正常,但是当我想使用 R 控制台时,它只绘制地图而不是路线。有人对此有解决方案吗?我的软件需要使用控制台而不是 RStudio 来执行。

谢谢!

library(rMaps)
#library(rCharts)
map = Leaflet$new()
map$setView(c(40.74119, -73.9925), 13)
map$tileLayer(provider = 'Stamen.TonerLite')

mywaypoints = list(c(40.74119, -73.9925), c(40.73573, -73.99302))

map$addAssets(
  css = "http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.css",
  jshead = "http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.min.js"
)

routingTemplate = "
<script>
var mywaypoints = %s
L.Routing.control({
waypoints: [
L.latLng.apply(null, mywaypoints[0]),
L.latLng.apply(null, mywaypoints[1])
]
}).addTo(map);
</script>"

map$setTemplate(
  afterScript = sprintf(routingTemplate, RJSONIO::toJSON(mywaypoints))
)
map$set(width = 800, height = 800)
map

闪亮的代码如下

library(rCharts)
library(shiny) 

runApp(list(
  ui = pageWithSidebar(
    headerPanel("Title"),
    sidebarPanel("MyApp" ),
    mainPanel(
      tabsetPanel(
        tabPanel("Interactive Map", chartOutput("myChart", 'leaflet'))
      )
    )
  ),
  server = function(input, output){
    output$myChart <- renderMap({
      map = Leaflet$new()
      map$setView(c(40.74119, -73.9925), 13)
      map$tileLayer(provider = 'Stamen.TonerLite')

      mywaypoints = list(c(40.74119, -73.9925), c(40.73573, -73.99302))

      map$addAssets(
        css = "http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.css",
        jshead = "http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.min.js"
      )

      routingTemplate = "
<script>
var mywaypoints = %s
L.Routing.control({
waypoints: [
L.latLng.apply(null, mywaypoints[0]),
L.latLng.apply(null, mywaypoints[1])
]
}).addTo(map);
</script>"

      map$setTemplate(
        afterScript = sprintf(routingTemplate, RJSONIO::toJSON(mywaypoints))
      )
      map$set(width = 800, height = 800)
      map
    })
  }
))
4

1 回答 1

0

为了让闪亮的代码正常工作,我做了一些调整(并且样式有点偏离......)

library(rCharts)
library(shiny) 

runApp(list(
    ui = pageWithSidebar(
        headerPanel("Title"),
        sidebarPanel("MyApp" ),
        mainPanel(
            tabsetPanel(
                tabPanel("Interactive Map", 
                             chartOutput("baseMap", 'leaflet'),
                             tags$head(tags$style("http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.css")),
                             tags$head(tags$script(src="http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.min.js")),
                             uiOutput("routeMap"))
            )
        )
    ),

    server = function(input, output){

        output$baseMap <- renderMap({
            baseMap = Leaflet$new()
            baseMap$setView(c(40.74119, -73.9925), 13)
            baseMap$tileLayer(provider = 'Stamen.TonerLite')
            baseMap$set(width = 800, height = 800)
            baseMap
        })

        output$routeMap <- renderUI({

            mywaypoints = list(c(40.74119, -73.9925), c(40.73573, -73.99302))
            tags$body(tags$script(HTML(sprintf(
                "var mywaypoints = %s  
                L.Routing.control({
                waypoints: [
                    L.latLng.apply(null, mywaypoints[0]),
                    L.latLng.apply(null, mywaypoints[1])
                ] }).addTo(map)", RJSONIO::toJSON(mywaypoints)
            ))))
        })
    }
))

这适用于 RStudio 和控制台。不过,我还没有 .R 脚本的解决方案。

于 2015-10-27T00:40:38.567 回答