Use a custom font within shinyapps.io?

I have the following code (placed at the top of my app.R file, just after my library() calls):

dir.create('r-lib')

download.file('https://cran.r-project.org/src/contrib/extrafontdb_1.0.tar.gz','r-lib/extrafontdb_1.0.tar.gz')

.libPaths(c('r-lib', .libPaths()))

install.packages('r-lib/extrafontdb_1.0.tar.gz',type = 'source',repos = NULL)

dir.create('~/.fonts')

download.file("https://github.com/** GIT USER NAME **/fonts/archive/master/fonts.zip", destfile = "fonts.zip")

unzip("fonts.zip", exdir = "~/.fonts")

extrafont::ttf_import("~/.fonts")

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

gg_set_the_theme()

The above aims to:

  • Download and install extrafontdb (as i want to be able to save and access fonts to this db)
  • Create a .fonts directory
  • Download my custom fonts from Github
  • Import the .ttf files to the db
  • Tell the system where the fonts are stored

The last line is a function as follows:

gg_set_the_theme <- function(base_size=10, base_family="Sarabun") {

 extrafont::loadfonts("win", quiet = TRUE)

 ggplot2::theme_set(gg_the_theme(base_size=10, base_family=base_family))
}

Note: "gg_the_theme" is just a function to style colours and sizes within "ggplot2::theme".

I then publish to shinyapps.io and it fails to deploy with this error message:

trying URL 'https://cran.r-project.org/src/contrib/extrafontdb_1.0.tar.gz'
Content type 'application/x-gzip' length 1672 bytes
==================================================
downloaded 1672 bytes

Installing package into ‘/srv/connect/apps/** APP NAME **/r-lib’
(as ‘lib’ is unspecified)
* installing *source* package ‘extrafontdb’ ...
** package ‘extrafontdb’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (extrafontdb)
trying URL 'https://github.com/** GIT USER NAME **/fonts/archive/master/fonts.zip'
downloaded 2.9 MB

Scanning ttf files in ~/.fonts ...
Extracting .afm files from .ttf files...
Found FontName for 19 fonts.
Scanning afm files in /srv/connect/apps/** APP NAME **/r-lib/extrafontdb/metrics
Writing font table in /srv/connect/apps/** APP NAME **/r-lib/extrafontdb/fontmap/fonttable.csv
Writing Fontmap to /srv/connect/apps/** APP NAME **/r-lib/extrafontdb/fontmap/Fontmap...
Error in value[[3L]](cond) : could not find function "windowsFonts"
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted

However, it looks as though the fonts i wanted to import are there (as my github file contains 19 .ttf files). I'm not sure why or where this "windowsFonts" function would be required. Perhaps there is an issue with the fact my code is designed for windows and not linux (as this is what shinyapps.io uses)?

Any help would be greatly appreciated.

Thanks, Matt

Duplicated over here a week ago,
https://stackoverflow.com/questions/62771749/use-a-custom-font-within-shinyapps-io

Folks should be aware of our cross post policy, FAQ: Is it OK if I cross-post?

Thanks EconomiCurtis, i posted here only after i had no response on SO. I hope this is okay. Matt