【问题标题】:Getting Trailing Garbage error when loading maps from Highcharter in R从 R 中的 Highcharter 加载地图时出现尾随垃圾错误
【发布时间】:2022-01-14 09:17:18
【问题描述】:

我使用 R 中的 highcharter 包创建了一个带有一堆图表的仪表板。我还制作了一些带有突出显示的图块的地图。我按如下方式加载了地图:

library(highcharter)

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

这产生了一个数据框,其中包含一些有用的列以添加到我的主数据框中。绘图时:

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 中并将其转换为数据框。数据可以在这里找到:

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

好的,所以在查阅了地图数据的变更日志之后:

我认为这是我不幸的根源。知道可以做什么吗?

【问题讨论】:

    标签: javascript r json highcharts r-highcharter


    【解决方案1】:

    经过进一步调查,并在维护包的人@jbkunst 的帖子和回答之后,有一个修复需要拉取最新版本的包。

    这是代码:

    remotes::install_github("jbkunst/highcharter")
    

    更多信息可在此链接获得:

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

    编辑: 这已不再正确,当您从 jbkunst 的 github 存储库下载包的更新版本时,该包也适用于 flexdashboard

    但是,请注意,对于我的具体问题,它嵌入在 flexdashboard 的制作中,拉取最新版本的软件包似乎会引入错误,即第一个图正确显示,而以下图则没有.此错误不一致,因此它可能会或可能不会发生在您的身上。我在 github 页面上为该包添加了一个帖子,如果您有兴趣,请提供一个功能示例:

    编辑: 此问题已解决 https://github.com/jbkunst/highcharter/issues/744

    为了针对我的具体情况解决这个问题,我提取了最新版本的软件包,然后将地图数据保存为我需要的所有地图的 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")
    
    
    
    

    这一切都是通过将新版本的包中的 JS 地图保存为 excel(见上文),删除并重新安装旧版本的包,最后使用 JSON 数据而不是 JS 生成地图来实现的。

    所有地图都可以在这里找到:

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

    【讨论】:

      【解决方案2】:

      您可以尝试从 Highcharts JS API 导入此脚本,然后查看它是否正常工作。在这里你可以找到一篇解释如何在 R 中使用 JS 的文章:https://www.highcharts.com/blog/tutorials/working-with-highcharts-javascript-syntax-in-r/?fbclid=IwAR1o6Hxeq21KQ3C8TwVXKJjoqs2XBaXy3Ai2j3dK86c6LyQcVEtnX_kVHfA

      【讨论】:

      • 谢谢,我一定会看看这个!
      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 2021-12-04
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多