Loading Custom Fonts Into ShinyApps

Hi all,

I have struggled for the better part of a day installing fonts to use with my Shiny App deployed on ShinyApps.io, however I have not been able to make this work, and am reluctantly requesting assistance. I have followed the instructions from the solution from this stackoverflow post - r - ggplot with customized font not showing properly on shinyapps.io - Stack Overflow - which calls for the following in my R script:

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

I have created the www directory locally and added the fonts. These are the 2 fonts I'd like to have installed. However my app still returns an error after deploying to ShinyApps, indicating in the logs that:

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font family 'IndieFlower' not found in PostScript font database
font family 'IndieFlower-Regular' not found in PostScript font database
font family 'Indie Flower' not found in PostScript font database
font family 'FreightDisp Pro Bold' not found in PostScript font database
Error in value[3L] : invalid font type
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne ->
Execution halted

Note that I tried to import my fonts in my app using different names (IndieFlower-Regular, IndieFlower, Indie Flower, etc.) as a debugging effort. However, as can be seen in the logs, all of these return the error that they are not found in the PostScript font database.

On the deploy, I next included the following lines of code:

dir.create('~/.fonts')
file.copy("www/IndieFlower-Regular.ttf", "~/.fonts")
file.copy("www/GarageFonts - FreightDisp Pro Bold.ttf", "~/.fonts")
system('echo "Debug Help"')
system('ls ~/.fonts')
system('fc-cache -f -v ~/.fonts')
system('ls ~/.fonts')
system('fc-list')

...in order to help debug by seeing in the logs (a) if the fonts were sent to the ~/.fonts directory, and (b) which fonts are installed on the system. I also added -v to the fc-cache to get a verbose output. Here are the relevant logs from these commands:

from system('ls ~/.fonts')

GarageFonts - FreightDisp Pro Bold.ttf
IndieFlower-Regular.ttf

from system('fc-cache -f -v ~/.fonts)

/home/shiny/.fonts: caching, new cache contents: 2 fonts, 0 dirs
/var/cache/fontconfig: not cleaning unwritable cache directory
/home/shiny/.cache/fontconfig: cleaning cache directory
/home/shiny/.fontconfig: not cleaning non-existent cache directory
fc-cache: succeeded

the 2nd system('ls ~/.fonts')

GarageFonts - FreightDisp Pro Bold.ttf
IndieFlower-Regular.ttf

the relevant part of fc-list

/usr/share/fonts/type1/gsfonts/a010013l.pfb: URW Gothic L:style=Book
/home/shiny/.fonts/IndieFlower-Regular.ttf: Indie Flower:style=Regular
/home/shiny/.fonts/GarageFonts - FreightDisp Pro Bold.ttf: FreightDisp Pro,FreightDisp Pro Bold:style=Bold,Regular
/usr/share/texmf/fonts/opentype/public/tex-gyre-math/texgyrepagella-math.otf: TeX Gyre Pagella Math:style=Regular
/usr/share/fonts/type1/gsfonts/d050000l.pfb: Dingbats:style=Regular

I'm not quite sure, but it does appear that my two fonts have been installed on the ShinyApps linux system?, however they are in a different location from all of the other fonts...:

  • in /home/shiny/.fonts,
  • rather than /usr/share/fonts or /usr/share/texmf/fonts, where other fonts are

Any help or thoughts on this would be greatly appreciated. I have also attempted to use the extrafont library, however I run into a permission denied error when trying to run font_import() on the ShinyApps server.

Please let me know if I can provide any additional info on this. Uploading fonts to ShinyApps seems applicable to a wide audience, and it would be great to resolve this. Thanks in advance for any help,

I'm struggling with the same except on a Shiny Server. Did you ever find a solution? Thanks.

The solution that worked for me was to use the showtext package

https://cran.rstudio.com/web/packages/showtext/vignettes/introduction.html