I've come across yet another strange interaction between R's data frames and Unicode characters on Windows, this time involving knitr and rmarkdown.
Implicit printing works correctly
I'm trying to print an HTML table based on a data frame containing Unicode characters, as represented by this simple example:
---
title: "Unicode Print Test"
---
```{r, results='asis'}
library(knitr)
knitr::kable(data.frame(eta="\U03B7"), format="html")
```
This produces the output I want when the document is knitted to HTML, shown below:
Explicit printing does not
But in the real application, I need to print several tables from inside a for
loop, meaning I have to explicitly print()
the table:
```{r, results='asis'}
library(knitr)
x <- knitr::kable(data.frame(eta="\U03B7"), format="html")
print(x)
```
Now, the Unicode character is not printed correctly when the document is knitted to HTML:
What to do?
Why does this difference between implicit and explicit printing occur? At least when executed in the R console, both explicit and implicit printing calls the knitr:::print.knitr_kable()
function. I'm would guess it has something to do with the evalaute
function (from the package of the same name) which actually executes the code in knitr code chunks, but I can't figure out what.
Is there any way I can have my explicit print()
calls and get the correctly formatted output? I am aware of this locale workaround which seems to work for some other Unicode + Data Frame issues, but not this one.
EDIT: According to a knowledgeable commenter, this is a deep issue related to how and when R converts characters using the native Windows encoding prior to display. So, this is always going to be an issue when using the print
function when on Windows, unless base R changes significantly.
Updated question: Are there any other methods (besides print()
-ing) for getting a kable
object to display from inside expressions such as for-loops?
SessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 7 x64 (build 7601) Service Pack 1
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] knitr_1.20
##
## loaded via a namespace (and not attached):
## [1] compiler_3.5.1 backports_1.1.2 magrittr_1.5 rprojroot_1.3-2
## [5] tools_3.5.1 htmltools_0.3.6 yaml_2.2.0 Rcpp_0.12.18
## [9] stringi_1.1.7 rmarkdown_1.10 highr_0.7 stringr_1.3.1
## [13] digest_0.6.16 evaluate_0.11