I have recently began using knitr::spin() to quickly convert my code into into html reports and so far it has been amazing.
However, I have run into a problem when attempting to include Chinese characters in either text or the code sections. Here is a short example:
not_working.R
#' # Sample document
#'
#' Some text.
#' 还有汉字。
#+
print("Some text")
print("他喜欢吃炒面")
#'
To generate output, I use:
knitr::spin("not_working.R")
The characters are distorted in the following manner:
Sample document
Some text.
杩樻湁姹夊瓧銆
print("Some text")
## [1] "Some text"
print("浠栧枩娆㈠悆鐐掗潰")
## [1] "<U+00E4><U+00BB>–<U+013A>–<U+015B><U+0107><U+00AC><U+02D8><U+013A>
<U+0090><U+0083><U+00E7><U+201A>’é<U+0165><U+02D8>"
I have tried setting my locale (both within the script and within .Rprofile). This has allowed me to display the characters in RStudio and in its console output, but not in the cases above.
I also tried doing File/Save with Encoding --> UTF-8
I am using Windows 7.
This problem does not occur if I create an R notebook myself (which seems to suggest that the problem might be at .R to .md conversion). The following works fine:
working.Rmd
---
title: "R Notebook"
output: html_notebook
---
他喜欢吃炒面
```{r}
print("他喜欢吃炒面")
```
My ultimate aim is to import an (UTF-8 encoded) text file, do analysis and display the interim results, all within the html report.
Is there any way to fix this problem?
Since knitr 1.21, knitr::spin()
only accepts R scripts encoded in UTF-8.