Shiny 中的 R Leaflet Offline Tiles [英] R Leaflet Offline Tiles within Shiny

查看:19
本文介绍了Shiny 中的 R Leaflet Offline Tiles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Shiny 应用程序中加载离线/本地传单地图图块?我可以在交互式 R 会话中加载图块,如下所示这里,但我现在想尝试加载它们以在 Shiny 应用程序中使用.这是我到目前为止的一个例子.我认为这与通过 IP 和端口运行的 Shiny 以及需要通过 IP 和端口加载图块有关.我已经尝试了一些方法来更改 IP 和端口(使它们相同),如 这里 但还没有找到任何可行的方法.我也可以使用在线图块让它工作,但我需要它来处理本地地图图块.

Is it possible to load offline/local leaflet map tiles within a Shiny app? I am able to load the tiles in an interactive R session as shown here, but I now want to try and load them for use in a Shiny app. Here's an example of what I have so far. I'm thinking it has something to do with Shiny running through an IP and port and needing to load the tiles through an IP and port as well. I've tried a few things to change IPs and ports (making them the same) as explained here but haven't figured out anything that works. I can also get it to work using online tiles, but I need it to work with local map tiles.

library(shiny)
library(leaflet)
library(RColorBrewer)
library(RgoogleMaps)

options(shiny.port = 8000)

  (bwi <- getGeoCode("BWI;MD"))

df <- as.data.frame(rbind(bwi))
df$col <- c("orange")
df$name <- c("BWI")

icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = df$col
)
#################################################################################

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,
                style = "padding: 8px; background: #FFFFEE; opacity:.9",
    checkboxInput("markers", "Show Markers?", TRUE)
  )
)    
#################################################################################

server <- function(input, output, session) {

  output$map <- renderLeaflet({
    leaflet() %>% 
      addTiles(urlTemplate = "http:/localhost:8000/C:/Users/OTAD USER/Documents/mapTiles/ESRIWorldTopoMap/{z}_{x}_{y}.png") %>%
      setView(lat = bwi[1], lng = bwi[2], zoom = 8)
  })

  observe({
    proxy <- leafletProxy("map", data = df)

    # Remove/show any markers
    proxy %>% clearMarkers()
    if (input$markers) {
      proxy %>% addAwesomeMarkers(lat = df$lat, lng = df$lon,
                                  icon = icons, label = df$name)
    }
  })
}

#Put the ui and server together and run
runApp(shinyApp(ui = ui, 
         server = server), launch.browser=TRUE
)

推荐答案

1- 您必须通过使用 addResourcePath 在资源上提供别名"来授权 Shiny 为该文件夹中的图块提供服务.

1- You have to authorize shiny to serve tiles in that folder by providing an "alias" on the ressource with addResourcePath

2- 然后将该别名用作 addTiles

2- then use that alias as the base URL in addTiles

server <- function(input, output, session) {
    addResourcePath("mytiles", "C:/Users/OTAD USER/Documents/mapTiles/ESRIWorldTopoMap")
    output$map <- renderLeaflet({
      leaflet() %>% 
        addTiles(urlTemplate = "/mytiles/{z}_{x}_{y}.png") %>%
        setView(lat = bwi[1], lng = bwi[2], zoom = 8)
    })
...

这篇关于Shiny 中的 R Leaflet Offline Tiles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆