treemap renders locally but not on shinyapps.io

Update:
I believe it's an issue with Unicode letters inside the render as after messing around, now the treemap is displayed but the text has "glyph missing" symbols.

Hi!

I'm making an interactive map of Japanese income data, and have a side panel displaying a treemap with the distribution of workers across sectors for a selected municipality.

This works locally, but does not render when I upload it to shinyapps.io giving the following error: ERROR: An error has occured. Check your logs or contact the app author for clarification.
However, no logs are present on my dashboard for this app.

Here's a link to the app:
https://andreasandersen.shinyapps.io/Japan_Income/

I can add the whole code, but I'll first paste the parts I think are relevant:

Packages loaded:

library(tidyverse)
library(treemap)
library(rgeos)
library(sf)
library(shiny)
library(shinythemes)
library(leaflet)
library(DT)
library(scales)
library(htmltools)

The function drawing the treemap given a data frame and the number of sectors to display in the treemap. (get_sector_names() is just a function converting the sector names to Japanese label values)

draw_treemap <- function(x, n) {
  temp_labourforce <- x %>% select(LabourForce) %>% pull()
  
  temp <- x %>% 
    select(Agriculture:Others) %>% 
    gather(key = "Sector", value = "Employees", Agriculture:Others) %>%
    mutate(Sector = get_sector_names(Sector)) %>% 
    arrange(desc(Employees))
  
  temp_others <- sum(temp$Employees[(n+1):nrow(temp)])
  
  temp_final <- temp %>% 
    head(n) %>% 
    rbind(data.frame(Sector = "その他産業\n就業率合計", Employees = temp_others)) %>% 
    mutate(Order = 1:(n+1)) %>% 
    mutate(Sector = paste(Sector, as.character(scales::percent(Employees / temp_labourforce , accuracy = 0.01)), sep = "\n"))
  
  return(treemap(temp_final, index = "Sector", vSize = "Employees", palette = "Set3", sortID = "Order", algorithm = "pivotSize",
          align.labels = c("left", "top"), fontface.labels = "plain", fontsize.labels = 15, border.lwds = 1, title = ""))
}

Reactive filtering function based on where the map is clicked

municipality_data <- reactive(if(is.null(input$map_shape_click) == TRUE) {
    df %>% filter(Municipality == "港区") %>% st_set_geometry(NULL)
  } else {
    if(is.character(input$map_shape_click$id) == TRUE) {
        df %>% filter(Code == input$map_shape_click$id) %>% st_set_geometry(NULL)
    } else {
        df %>% filter(Population == input$map_shape_click$id) %>% st_set_geometry(NULL)
      } 
  })

renderPlot with the call to the previous function

output$industrytreemap_plot <- renderPlot({
    draw_treemap(municipality_data(), 7)
  })

Just to clarify, all this works locally.
I'd appreciate any and all ideas or suggestions on this.

After some trial and error and searching around, I have been able to make it work and perhaps, found the reason behind it all.

It seems like shinyapps for some reason does not have a font able to type Japanese characters available to be used inside the treemaps package, and possibly others as well.

treemaps calls the gpar() function to render the labels with a specific font family given in the fontfamily.label = argument. Locally, it would seem like it found an appropriate Unicode supported font, but this would apparently not work once it was uploaded to shinyapps.

I added the following code to add a font with Japanese characters to shinyapps server, (Link for details) and now my treemaps work without a hitch.

dir.create('~/.fonts')
file.copy("www/test.ttf", "~/.fonts")
system('fc-cache -f ~/.fonts')

No need to specify a font family in the treemap() function either, the tree would now render correctly with the following code going through renderPlot():

treemap(temp_final, index = "Sector", vSize = "Employees", palette = "Set3", sortID = "Order", algorithm = "pivotSize",
          align.labels = c("left", "top"), fontface.labels = "plain", fontsize.labels = 15, border.lwds = 1, title = "")

https://andreasandersen.shinyapps.io/Japan_Income/

In general, I have to say working with Japanese characters within R and Shiny have been a mixed experience. Most things work as you'd expect, others require some workaround. This issue, however, I feel like it should have been addressed by the developers, or be documented somewhere...

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.