Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom fonts on shinyapps.io

I would like to use a custom font in my shiny app (on plots) on shinyapps.io. I have my Roboto-Regular.ttf in the ./www/ directory. And this is the upper portion of my app.R file:

dir.create('~/.fonts')
system("chmod +x ./www/Roboto-Regular.ttf")
system("cp ./www/Roboto-Regular.ttf ~/.fonts/")
system('fc-cache -f -v ~/.fonts/')
system('fc-match Roboto')

library(ggplot2)
library(shiny)
library(shinythemes)

library(extrafont)
font_import(pattern="Roboto",prompt=FALSE)
loadfonts()
print(fonts())

Upon deploying the app, I end up with an error that looks like this:

Registering fonts with R
Scanning ttf files in /usr/share/fonts/, ~/.fonts/ ...
Extracting .afm files from .ttf files...
/home/shiny/.fonts/Roboto-Regular.ttfWarning in gzfile(dest, "w") :
  cannot open compressed file '/opt/R/3.5.1/lib/R/library/extrafontdb/metrics/Roboto-Regular.afm.gz', probable reason 'Permission denied'
Error in value[[3L]](cond) : cannot open the connection
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted

Does anyone see what might be wrong?

like image 569
rmf Avatar asked Mar 05 '23 15:03

rmf


1 Answers

After a bit of struggle I found an even simpler solution that works on shinyapps.io:

Here we go:

  1. Place custom font in www directory: e.g. IndieFlower.ttf from here
  2. Follow the steps from here

This leads to the following upper part of the app.R file:

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

Since Linux looks into the .fonts directory to search fonts, you don't need the extrafont package, but you can directly use those fonts like:

ggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) +
      geom_line(position="jitter", color="red", size=2) + theme_bw() +
      theme(text=element_text(size = 16, family = "IndieFlower"))
like image 80
symbolrush Avatar answered May 24 '23 09:05

symbolrush