Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode with knitr and Rmarkdown

Is there a set of best practices or documentation for working with Unicode in knitr and Rmarkdown? I can't seem to get any glyphs to show up properly when knitting a document.

For example, this works in the console (in Rstudio):

> cat("\U2660   \U2665  \U2666  \U2663")
♠   ♥   ♦   ♣

But when knitting I get this:

  • HTML

enter image description here

  • Word

enter image description here

like image 565
user2987808 Avatar asked May 24 '17 08:05

user2987808


People also ask

How can you compile the R Markdown document using knitr package?

The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS). Under the hood, RStudio calls the function rmarkdown::render() to render the document in a new R session.

Can you run code in R Markdown?

You can open it here in RStudio Cloud. or by typing the chunk delimiters ```{r} and ``` . When you render your . Rmd file, R Markdown will run each code chunk and embed the results beneath the code chunk in your final report.

What is knitr used for?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.

How do I add code to R Markdown?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).

What is the default encoding for R Markdown?

The R markdown document is saved in the Windows system encoding (ISO-8859-1) by default. knitr and rmarkdown are slowly transitioning to UTF-8 only ( @yihui lists supporting other encodings as his biggest knitr regret ); the IDE should start defaulting new R Markdown content to UTF-8 for compatibility with these packages (and other tooling).

How does R work with knitr?

the "Knit" button invokes the render function, which sends the .Rmd file file to knitr, which executes the R code, and then sends the file to markdown, which

How do I Knit a RMD file in R?

the "Knit" button invokes the render function, which sends the .Rmd file file to knitr, which executes the R code, and then sends the file to markdown, which formats the combined files using pandoc to some sort of "document-p-code," and then passes the document to tinytex/TinyTex to compile the .tex document, which then

How do I convert a RMD file to Markdown?

the "Knit" button invokes the render function, which sends the .Rmd file file to knitr, which executes the R code, and then sends the file to markdown, which formats the combined files using pandoc to some sort of "document-p-code," and then


2 Answers

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.

like image 132
srvanderplas Avatar answered Sep 19 '22 12:09

srvanderplas


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

like image 45
stevec Avatar answered Sep 18 '22 12:09

stevec