问题
I would like to generate the map in shiny (attached), however I am having trouble plotting the map from the code below. Could you take a look and help me solve it ??. The shapefile files can be downloaded from the following website: https://github.com/ropensci/stplanr/releases/download/0.6.1/Example.zip
If you do not have the sfnetworks package installed, please check this website: https://github.com/luukvdmeer/sfnetworks
Thank you very much!
library(shiny)
library(sf)
library(sfnetworks)
library(shinythemes)
roads = st_read("Example/Roads/Roads.shp", quiet = TRUE)
points = st_read("Example/Points/Points.shp", quiet = TRUE)
roads_trf = st_transform(roads, st_crs(points)) %>%
st_cast("LINESTRING")
net = as_sfnetwork(roads_trf, directed = FALSE) %>%
activate("edges") %>%
dplyr::mutate(weight = edge_length())
# routing
p = sf::st_as_sf(data.frame(x = to[1], y = to[2]), coords = c("x", "y"), crs = sf::st_crs(net))
r = net %>%
tidygraph::convert(to_spatial_shortest_paths, points[17, ], points[4, ])
# plot
plot(net)
plot <-plot(r, col = "blue", lwd = 5, add = TRUE)
ui <- bootstrapPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Cl",
tabPanel("Solution",
sidebarLayout(
sidebarPanel(
),
mainPanel(
tabsetPanel(
tabPanel("Solution", (plotOutput(("Map"))))))
))))
server <- function(input, output, session) {
output$Map <- renderPlot({
})
}
shinyApp(ui = ui, server = server)
回答1:
output$Map <- renderPlot({
plot(net)
plot(r, col = "blue", lwd = 5, add = TRUE)
})
来源:https://stackoverflow.com/questions/62878163/show-map-in-shiny