Search code examples
rfonts

Importing the xkcd fonts in R (for xkcd package) on windows


I am not used to answer my own question. However, I thought the solution could benefice someone in the future (see answer). It was not possible to post this issue on the package's github (a read-only mirror).

I had a lot of trouble to install the xkcd font in R on windows, especially for recent version of R (4.1.1). There is this solution on OS, which do not work on windows. It needed this answer and this answer (two unrelated questions) to be able to use the font in R.


Solution

  • Here is the solution I reached.

    Download necessary packages.

    install.packages("extrafont","remotes","xkcd")
    library(extrafont)
    library(remotes)
    

    One of the problem is the error message In system2(ttf2pt1, c(args, shQuote(ttfiles[i]), shQuote(tmpfiles[i])), : running command [...] that is resolved by downgrading Rttf2pt1 to version 1.3.8

    remotes::install_version("Rttf2pt1", version = "1.3.8")
    

    Download the font and be sure it is in the working directory (it should be).

    download.file("http://simonsoftware.se/other/xkcd.ttf", dest = "xkcd.ttf", mode = "wb")
    

    The original description to import the font does not work (see the vignette("xkcd-intro"), the path has to be redirected to the working directory.

    font_import(paths = getwd(), pattern = "[X/x]kcd", prompt = FALSE)
    

    Loading the font and the xkcd package should now work.

    loadfonts(device = "win")
    library(xkcd)
    

    The package xkcd and its functions should now work properly.