Search code examples
javascriptrjsonhighchartsr-highcharter

Getting Trailing Garbage error when loading maps from Highcharter in R


I have created a Dashboard with a bunch of graphs using the highcharter package in R. I've also produced some maps with highlighted tiles. I loaded the maps as follows:

library(highcharter)

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

Which yielded a dataframe containing some useful columns to add to my main dataframe. When plotting:

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}"
    ))

However, since today, I get the following error:

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) ------^

I don't know if that semi-colon was added, but it seems to return an error on all of my maps. Do you know why? Alternatively, is there a way to load the javascript into R and transform it into a dataframe. The data can be found here:

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

Alright, so after consulting the changelogs to the map data:

enter image description here

I assume this is the source of my misfortune. Any idea about what can be done?


Solution

  • After further investigation, and following a post and answer by the person maintaining the package, @jbkunst, there is a fix that requires to pull the latest version of the package.

    This is the code:

    remotes::install_github("jbkunst/highcharter")
    

    Additional information is available at this link:

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

    EDIT: This is no longer true, when you download the updated version of the package from jbkunst's github repository, the package works with flexdashboard as well.

    However, please note that for my specific problem, which was embedded in the production of a flexdashboard, pulling the latest version of the package seems to introduce an error whereby the first plot appears properly, and the following plots do not. This error is not consistent, so it may or may not occur on your end. I have added a post on the github page for the package, with a functional example if you're interested:

    EDIT: This issue has been resolved https://github.com/jbkunst/highcharter/issues/744

    In order to solve this issue for my specific case, I pulled the newest version of the package, then saved the map data as an excel file for all the maps I needed. I then relied on the JSON versions, which do work, to produce the maps: Here's an example with tiles:

    
    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)
    
    

    enter image description here

    And here's an example with City bubbles:

    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")
    
    
    
    

    enter image description here

    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.

    All maps can be found here:

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