gfonts"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>", 
  eval = FALSE
)
library(gfonts)

{gfonts} allow you to use a Google font to use it offline in a Shiny application or a R Markdown document. You can download a font via google-webfonts-helper and generate appropriate CSS to use it.

Setup a font to use in your project

In your project directory, use setup_font once to download a font and generate CSS code. For example to use the Roboto font, you can do :

setup_font(
  id = "roboto",
  output_dir = "www",
  variants = "regular"
)

In a Shiny application you can use www/ folder, for R Markdown, create a sub-folder at the same level as your .Rmd file.

setup_font() will create two sub-folders, containing the following files :

www
+-- css
|   \-- roboto.css
\-- fonts
    +-- roboto-v20-latin-regular.eot
    +-- roboto-v20-latin-regular.svg
    +-- roboto-v20-latin-regular.ttf
    +-- roboto-v20-latin-regular.woff
    \-- roboto-v20-latin-regular.woff2

To know all fonts and their ids, you can use get_all_fonts() :

head(get_all_fonts())

Use a font

To use a downloaded font, you can use in your UI or in a chunk :

use_font("roboto", "www/css/roboto.css")

First argument is the id of font downloaded, second is path to CSS file generated.

An other solution in Shiny application is to import the CSS file in a link tag and add a style tag:

fluidPage(

  tags$link(rel = "stylesheet", type = "text/css", href = "css/roboto.css"),
  tags$style("body {font-family: 'Roboto', sans-serif;}")

)

In Markdown, import CSS file in yaml header, and add a CSS chunk :

---
output: 
  html_document:
    css: assets/css/roboto.css
---

```{css}
body {font-family: 'Roboto', sans-serif;}
```

Download a font

If you only want to download a font, you can use:

download_font(
  id = "roboto",
  output_dir = "azerty",
  variants = c("regular", "300italic", "700")
)

Generate CSS

To download CSS code to import a font in HTML, you can use:

generate_css("roboto", "regular", font_dir = "path/to/font")
cat(generate_css("roboto", "regular", font_dir = "path/to/"))

The path must be relative to the one were this code is saved.



Try the gfonts package in your browser

Any scripts or data that you put into this service are public.

gfonts documentation built on Jan. 13, 2021, 4:35 a.m.