Unicode with knitr and Rmarkdown

It looks like an encoding issue specific to Windows, and may be related to this issue: https://github.com/hadley/evaluate/issues/59 Unfortunately we have to wait for a fix in base R, but if you don't have to use cat(), and this expression is a top-level expression in your code chunk (e.g. not inside a for-loop or if-statement), I guess this may work:

knitr::asis_output("\U2660   \U2665  \U2666  \U2663")

It passes the character string directly to knitr and bypasses cat(), since knitr cannot reliably catch multibyte characters written out by cat() on Windows -- it depends on whether the characters can be represented by your system's native encoding.


For anyone else who came across this after trying to get emoji support in Rstudio/Rmarkdown documents, another possible issue is that if the file encoding isn't set to UTF-8, the resulting compiled document won't support emojis either.

In order for emoji to work in Rmarkdown, you must change the file encoding of the Rmd document. Go to File -> Reopen with encoding, then select UTF-8.

Once you have ensured the file is open in UTF-8 encoding, you should be able to compile with emoji support.

You should even be able to paste emoji from a browser directly into the document. 😺

It is probably a good idea to change the default encoding for all files to UTF-8 so that you don't have to deal with this issue again.


Unicode: Inline

Phew, that was close `r knitr::asis_output("\U1F605  \U2660   \U2665  \U2666  \U2663")` 

enter image description here

Unicode: Block

```{r, echo=FALSE} 
knitr::asis_output("Phew, that was close \U1F605  \U2660   \U2665  \U2666  \U2663")
```

enter image description here

The emo package

Unfortunately, this package isn't yet on CRAN, but it can be installed with devtools::install_github("hadley/emo")

emo::ji("face")

There are some more examples here