如何在Mapdeck中使用set_token?

时间:2019-06-19 14:22:24

标签: r mapdeck

我正在尝试使用mapdeck r软件包重现此示例:https://github.com/SymbolixAU/mapdeck

我在mapbox网站上注册了自己并创建了令牌。 每当我运行脚本时,都不会出错,但是也没有地图。

library(mapdeck)
library(leaflet)

key <- set_token("pk.eyJ1IjoicmFxdWVsc2FyYWl2YTE5ODgiLCJhIjoiY2p4MzM2eHh5MG95aTN5cDQxdjVocDlxMCJ9.Wskus8QqYwjAufGpW71OVg")

df <- readRDS("df.rds")

df$Station_Long = as.numeric(as.character(df$lon_Pay))
df$Station_Lat = as.numeric(as.character(df$lat_Pay))
df$id = as.factor(as.numeric(as.factor(df$ID)))

mapdeck(
  token = key,
  style = mapdeck_style('dark')
  , location = c(104, 1)
  , zoom = 8
  , pitch = 45
) %>%
  add_arc(
    data = df
    , origin = c("centroid_lon", "centroid_lat")
    , destination = c("lon_Pay", "lat_Pay")
    , layer_id = 'arclayer'
    , stroke_width = 3
    , stroke_from = "#ccffff"
    , stroke_to = "#ccffff"
  )

我的密钥是NULL(空)。

有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

如果使用set_token(),则不会将其分配给变量,而只需调用它即可。

library(mapdeck)

set_token( "YOUR_MAPOBX_TOKEN" )

然后将其全局存储在您的会话中

## view your token
mapdeck_tokens()

# Mapdeck tokens
# -  mapbox : YOUR_MAPBOX_TOKEN

使用set_token()意味着您不必在token调用中提供mapdeck()参数

url <- 'https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv'
flights <- read.csv(url)
flights$id <- seq_len(nrow(flights))
flights$stroke <- sample(1:3, size = nrow(flights), replace = T)
flights$info <- paste0("<b>",flights$airport1, " - ", flights$airport2, "</b>")

mapdeck( style = mapdeck_style("dark"), pitch = 45 ) %>%
  add_arc(
    data = flights
    , layer_id = "arc_layer"
    , origin = c("start_lon", "start_lat")
    , destination = c("end_lon", "end_lat")
    , stroke_from = "airport1"
    , stroke_to = "airport2"
    , stroke_width = "stroke"
    , tooltip = "info"
    , auto_highlight = TRUE
    , legend = T
    , legend_options = list(
      stroke_from = list( title = "Origin airport" ),
      css = "max-height: 100px;")
   )

enter image description here