Custom fonts in R Markdown with shinyapps.io

Hello,

my R Markdown document can be downloaded from a shiny application. I can set the custom font of the document (Times) in different ways using the fontspec package and the subsequent "setmainfont" command. In fact I can specify directly the font name "Times" or I can give to this function the local path to my .otf files (normal and bold font).

The problem is that when the application is deployed on shinyapps.io every method just doesn't work. I don't know which path to specify if I want to upload my fonts online with the application. It seems that for the R markdown document embedded in a shiny app it's not possible to specify the path as I would do for normal files (datasets, functions.rda) presents in some subfolders of my application.

Any suggestion? Thanks!

Have you tried a relative path?

For example, if, in the directory that contains your application, you have a sub-directory named "fonts", reference the fonts with "./fonts/" at the beginning.

Thank you for the reply Josh,

I followed all the instruction to generate a report with R Markdown that can be downloaded through a shiny app.

When I work locally, inside the YAML of the .rmd file I write this code:

---
output:
  pdf_document:
    latex_engine: xelatex
header-includes:
  - \usepackage{fontspec}
  - \setmainfont{Times}
---

and I am able to download the report with the desired font. I need the “xelatex” engine to make the functions of the fontspec package working properly.

When I use this function locally, I don’t need any .otf file (normal and bold). Moreover, locally I can also not use the fontspec package and its main function “setmainfont”, because I can also just specify in the YAML mainfont{Times} and everything works again.

When I deploy the app online these functions don’t work.

Hence, I downloaded the .otf files for the Times font, I put them in the App folder of my shiny app (or a subdirectory) and I specified the path inside the YAML always using the fontspec package, this time through this code:

---
output:
  pdf_document:
    latex_engine: xelatex
header-includes:
  - \usepackage{fontspec}
  - \setmainfont[Path = \string~/inst/App/, BoldFont = TimesBold, Extension = .otf]{Times}
---

Again, this command works when I use the application locally, but not on the shinyapps.io server.

It seems that things work differently for a markdown report, because I can’t specify a path like I do for the files that I need in my shiny app (as dataset, modules, etc.).

Do you think I should try with the "." before the subdirectory? Thanks a lot.

~/inst/App/ is not a relative path. ~ is expanded to the user's home directory. This works fine locally, but not on shinyapps.io, because the home directory is not where the application is deployed. See the documentation for some examples of absolute versus relative path usage.

1 Like

Hi,
Did you get a solution to your problem? I asked the same question before I stumbled upon your query.

The solution is described in a post of StackExchange https://tex.stackexchange.com/a/516382

Summary:
Include the font as TTF or OTF file in your Shiny app along with the Rmd file.
Also copy the font file when copying the Rmd file to a temp directory before processing.
In the Rmd refer to the font by file name. Since the font is in the same directory, no path should be necessary.

Yes, I posted the question also on StackExchange and StackOverflow, so at the link that dessy shared you can find useful information to resolve the problem.

If you want, you can check this question where I summarised the correct answer given on StackExchange: https://stackoverflow.com/questions/31776727/using-extra-fonts-with-rstudios-shiny-when-downloading-reports-latex/58857753#58857753.

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