How to add polylines from one place to another separately, using a leaflet in a shiny form?

I am trying to add polylines from one specific place to many others in brilliant R using addPolylinesfrom leaflets . But instead of connecting from one place to another, I can only link them all together in sequence. The best example of what I'm trying to achieve can be seen here in the diagram of the cricket wheeled wagons .

observe({
  long.path <- c(-73.993438700, (locations$Long[1:9]))
  lat.path <- c(40.750545000, (locations$Lat[1:9]))
  proxy <- leafletProxy("map", data = locations)
  if (input$paths) {
     proxy %>% addPolylines(lng = long.path, lat = lat.path, weight = 3, fillOpacity = 0.5, 
                       layerId = ~locations, color = "red")
  }
})

It is in a reactive expression, since I want them to be activated using a checkbox.

I would really appreciate any help with this!

+4
source share
2 answers

, mapview. SpatialLines, ( locations), bind mapview.

library(mapview)
library(raster)

## start point
root <- matrix(c(-73.993438700, 40.750545000), ncol = 2)
colnames(root) <- c("Long", "Lat")

## end points
locations <- data.frame(Long = (-78):(-70), Lat = c(40:44, 43:40))

## create and append spatial lines
lst <- lapply(1:nrow(locations), function(i) {
  SpatialLines(list(Lines(list(Line(rbind(root, locations[i, ]))), ID = i)), 
               proj4string = CRS("+init=epsg:4326"))
})

sln <- do.call("bind", lst)

## display data
mapview(sln)

lines

Line -to- SpatialLines (. ?Line, ?SpatialLines).

+2

, , , , .

, addPolyline . , , 9 . . , .

dest_df <- data.frame (lat = c(41.82, 46.88, 41.48, 39.14),
                   lon = c(-88.32, -124.10, -88.33, -114.90)
                  )

(4 ) . . ,

orig_df <- data.frame (lat = c(rep.int(40.75, nrow(dest_df))),
                   long = c(rep.int(-73.99,nrow(dest_df)))
                  )

, , , addPolylines . , , , , , , . DataFrame , , :

- 1 - - 2 - ...

, . 1 2 (, 1 3 5 7). 2 2 (, 2, 4, 6, 8). 2 UNION all. , . sqldf , . , .

orig_df$sequence <- c(sequence = seq(1, length.out = nrow(orig_df), by=2))
dest_df$sequence <- c(sequence = seq(2, length.out = nrow(orig_df), by=2))

library("sqldf")
q <- "
SELECT * FROM orig_df
UNION ALL
SELECT * FROM dest_df
ORDER BY sequence
"
poly_df <- sqldf(q)

dataframe : ,

, , :

library("leaflet")
leaflet() %>%
  addTiles() %>%

  addPolylines(
    data = poly_df,
    lng = ~lon, 
    lat = ~lat,
    weight = 3,
    opacity = 3
  ) 

, , , , -

+1

All Articles