1

I am following this example:

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

My goal is to plot a route. Using RStudio it is working properly but when I want to use the R Console it is only plotting the map but not the route. Anyone has a solution for this? My software needs to be executed using the Console and not RStudio.

THanks!

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

THe Shiny code is the following

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
    })
  }
))
aruizga
  • 644
  • 1
  • 4
  • 19

1 Answers1

0

To get the shiny code working I've made a couple of tweaks (and the styling is a bit off...)

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)
            ))))
        })
    }
))

This works from both RStudio and the Console. I don't have a solution for the .R script yet though.

tospig
  • 7,762
  • 14
  • 40
  • 79