簡體   English   中英

從 R 中的 Highcharter 加載地圖時出現尾隨垃圾錯誤

[英]Getting Trailing Garbage error when loading maps from Highcharter in R

我使用highcharter中的 highcharter package 創建了一個帶有一堆圖表的儀表板。 我還制作了一些帶有突出顯示的圖塊的地圖。 我按如下方式加載了地圖:

library(highcharter)

mapdata <- get_data_from_map(download_map_data("custom/world-highres.js"))

這產生了一個 dataframe 包含一些有用的列,以添加到我的主要 dataframe 中。 繪圖時:

hcmap("custom/world-highres.js", showInLegend = FALSE) %>%
  hc_add_series(
    data = df, 
    type = "mapbubble",
    name = "city", 
    minSize = "1%",
    maxSize = "5%",
    tooltip = list(
      pointFormat = "{point.city}: {point.z:,.0f}"
    ))

但是,從今天開始,我收到以下錯誤:

hcmap("custom/world-highres.js")
trying URL 'https://code.highcharts.com/mapdata/custom/world-highres.js'
Content type 'text/javascript' length 238592 bytes (233 KB)
downloaded 233 KB

Error: parse error: trailing garbage
          [6810,7337],[6838,7338]]]}}]};
                     (right here) ------^

我不知道是否添加了分號,但它似乎在我的所有地圖上都返回了錯誤。 你知道為什么嗎? 或者,有沒有辦法將 javascript 加載到 R 並將其轉換為 dataframe。 數據可以在這里找到:

https://code.highcharts.com/mapdata/custom/world-highres.js

好的,所以在查閱 map 數據的更改日志后:

在此處輸入圖像描述

我想這是我不幸的根源。 關於可以做什么的任何想法?

您可以嘗試從 Highcharts JS API 導入此腳本,然后查看它是否正常工作。 Here you can find an article explaining how to work with JS in R: https://www.highcharts.com/blog/tutorials/working-with-highcharts-javascript-syntax-in-r/?fbclid=IwAR1o6Hxeq21KQ3C8TwVXKJjoqs2XBaXy3Ai2j3dK86c6LyQcVEtnX_kVHfA

經過進一步調查,並在維護 package 的人@jbkunst 的帖子和回答之后,有一個修復程序需要拉取最新版本的 package。

這是代碼:

remotes::install_github("jbkunst/highcharter")

此鏈接提供了更多信息:

https://github.com/jbkunst/highcharter/issues/741

但是,請注意,對於我在生產flexdashboard中嵌入的具體問題,拉動最新版本的 package 似乎會引入一個錯誤,即第一個 plot 會正確顯示,而以下繪圖則不會。 此錯誤不一致,因此它可能會或可能不會發生在您的身上。 我在 package 的 github 頁面上添加了一個帖子,如果您有興趣,可以提供一個功能示例:

https://github.com/jbkunst/highcharter/issues/744

為了針對我的具體情況解決這個問題,我提取了最新版本的 package,然后將 map 數據保存為 excel 文件,用於我需要的所有地圖。 然后,我依靠確實有效的 JSON 版本來生成地圖:這是一個瓷磚示例:


library(highcharter)
library(tidyverse)
library(jsonlite)
library(httr)
library(readxl)

de_map_js <- read_excel("your path/map.xlsx") # Opening JS Map from Excel


map_data <- as_tibble(data.frame(name = de_map_js$name,
                                 values = sample(c(50:200), 16, replace = T))) # Creating Fake Data by Region for Germany



de_map <- "https://code.highcharts.com/mapdata/countries/de/de-all.geo.json" %>% #Downloading the German Map as JSON
  GET() %>% 
  content()



highchart(type = "map") %>% 
  hc_title(text = "Title") %>%
  hc_subtitle(text = "Subtitle") %>%
  hc_add_series_map(map = de_map, df = map_data, joinBy = "name", value = "values", # map = JSON map ; df = data.frame with correct region names ; values = your data values
                    dataLabels = list(enabled = TRUE
                                      ,format = "{point.properties.hc-a2}") # Adding region initials on map
  ) %>%
  hc_colorAxis(stops = color_stops(colors = viridisLite::rocket(213, direction = -1,
                                                                begin = 0.2,
                                                                end = 1))) %>%
  hc_mapNavigation(enabled = TRUE)

在此處輸入圖像描述

這是城市泡沫的一個例子:

library(highcharter)
library(tidyverse)
library(jsonlite)
library(httr)
library(readxl)




df_fake <- data.frame(
  name = c("Cologne"),
  lat = c(50.9375),
  lon = c(6.9603),
  z = c(1)
) # Creating fake tile data

df <- data.frame(
  name = c("Berlin", "Frankfurt", "Munich", "Cologne"),
  lat = c(52.5200, 50.1109, 48.1351, 50.9375),
  lon = c(13.4050, 8.6821, 11.5820, 6.9603),
  z = c(1000, 500, 750, 1250)
) # Creating bubble map series


de_map_json <- "https://code.highcharts.com/mapdata/countries/de/de-all.geo.json" %>% #Downloading the German Map as JSON
  GET() %>% 
  content() 


highchart(type = "map") %>%
  hc_add_series_map(map = de_map_json, df = df_fake, joinBy = "name", value = "z") %>% # The fake data goes into the map generation, with the real map as JSON
  hc_add_series(
    data = df, # Here you can add your data
    type = "mapbubble", # The type of plot
    name = "city", 
    minSize = "1%",
    maxSize = "5%",
    tooltip = list(
      pointFormat = "{point.name}: {point.z:,.0f}"  # Hover options
    )) %>%
  hc_mapNavigation(enabled = TRUE) %>%
  hc_colorAxis(stops = color_stops(colors = viridisLite::turbo(10, begin = 0.4, end = 0.9))) %>%
  hc_title(text = "Title")



在此處輸入圖像描述

This was all achieved by savings the JS maps as excel from the newer version of the package (see above), deleting and reinstalling the old version of the package, and finally generating the maps using JSON data instead of JS.

所有地圖都可以在這里找到:

https://code.highcharts.com/mapdata/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM