Use a custom font within shinyapps.io?

删除回忆录丶 提交于 2020-07-10 03:12:49

问题


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

来源:https://stackoverflow.com/questions/62771749/use-a-custom-font-within-shinyapps-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!