问题描述
我使用Windows 7,R2.15.3和RStudio 0.97.320与knitr knitr_1.1.6(在Yihui后修复了'Encoding:knitr和子文件'问题,3月12日)
> sessionInfo()
R版本2.15.3(2013-03-01)
平台:x86_64-w64-mingw32 / x64(64位)
语言环境:
[1] LC_COLLATE =西班牙语_Argentina.1252 LC_CTYPE =西班牙语_Argentina.1252 LC_MONETARY =西班牙语_Argentina.1252
[4] LC_NUMERIC = C LC_TIME =西班牙语_Argentina.1252
附加的基础包:
[1] stats graphics grDevices utils数据集方法base
其他附加包:
[1] lattice_0.20-13 pixmap_0.4-11 RColorBrewer_1.0-5 ade4_1.5-1 pander_0 .3.1
[6] xtable_1.7-1
通过命名空间加载(未附加):
[1] digest_0.6.3 evaluate_0.4.3 formatR_0.7 grid_2。 15.3 knitr_1.1.6 stringr_0.6.2 tools_2.15.3
我有一个像这样的文件中的R代码:
## @knitr RunMyCode
print('从.R文件调用:áéíóúñ' )
#解决方法
my.text< - 'áéíóúñ'
编码(my.text)< - UTF-8
print(my.text)
我从Rmd文件调用它,例如:
标题
======================== ============================
西班牙语文本:áéíóúñ
使用它.Rmd代码:它出来了...
```{r}
print('áéíóúñ')
```
```{r ReadFunctions,cache = FALSE,echo = TRUE,eval = TRUE}
read_chunk('TestSpanishText.R')
```
西班牙语文本在这里出现乱码:
```{r RunMyCode,echo = TRUE,eval = TRUE,cache = TRUE,dependson = c('ReadFunctions')}
```$我的问题是用.R文件中输入的西班牙字符(这是在RStudio中编码的UTF-8)。b。 。如果在Rmd文件(父文件和子文件都工作良好)中输入这些字符,那么这些字符是正确的,但不是在R文件中。如下所示, Encoding()
确实提供了一个解决方法,但我想知道是否有另一种方法,如全局选项?如果我使用Encoding(),我得到RStudio控制台中的逆问题... 标题
西班牙语文本:áéíóúñ
使用它.Rmd代码:它出来了...
print(áéíóúñ )
## [1]áéíóúñ
read_chunk(TestSpanishText.R)
西班牙语文本在这里出现乱码:
打印(从.R文件调用:áÃÃóúñ)
## [1]从.R文件调用:áéÃóðñ
#解决方法
my.text< - áéóðñ
编码(my.text)< - UTF-8
print (my.text)
## [1]áéíóúñ
最好在 read_chunk()
,但由于您使用 UTF-8
,所以可能会有效: read_chunk(lines = readLines(TestSpanishText.R,encoding =UTF-8))
请先尝试此操作。如果它不工作,我将添加一个 encoding
参数。无论如何,我确定这肯定是有效的(只是稍长):
con = file(TestSpanishText.R ,encoding =UTF-8)
read_chunk(con)
close(con)
I am using Windows 7, R2.15.3 and RStudio 0.97.320 with knitr knitr_1.1.6 (downloaded after Yihui fixed the 'Encoding: knitr and child files' issue on March 12)
> sessionInfo()
R version 2.15.3 (2013-03-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=Spanish_Argentina.1252 LC_CTYPE=Spanish_Argentina.1252 LC_MONETARY=Spanish_Argentina.1252
[4] LC_NUMERIC=C LC_TIME=Spanish_Argentina.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lattice_0.20-13 pixmap_0.4-11 RColorBrewer_1.0-5 ade4_1.5-1 pander_0.3.1
[6] xtable_1.7-1
loaded via a namespace (and not attached):
[1] digest_0.6.3 evaluate_0.4.3 formatR_0.7 grid_2.15.3 knitr_1.1.6 stringr_0.6.2 tools_2.15.3
I have my R code in a file like this one:
## @knitr RunMyCode
print('Called from .R file: á é í ó ú ñ')
# Workaround
my.text <- 'á é í ó ú ñ'
Encoding(my.text) <- "UTF-8"
print(my.text)
I call it from a Rmd file such as this:
Title
========================================================
Spanish text: á é í ó ú ñ
Use it from .Rmd code: it comes out right...
```{r}
print('á é í ó ú ñ')
```
```{r ReadFunctions, cache=FALSE, echo=TRUE, eval=TRUE}
read_chunk('TestSpanishText.R')
```
Spanish text comes out garbled here:
```{r RunMyCode, echo=TRUE, eval=TRUE, cache=TRUE, dependson=c('ReadFunctions')}
```
My problem is with spanish characters typed in the .R file (which is UTF-8 encoded in RStudio). These characters are OK if typed in Rmd files (both parent and child files work well), but not in R files. As you can see below, Encoding()
does provide a workaround, but I wonder if there is another way, such as a global option? If I use Encoding(), I get the inverse problem in the RStudio console...
Title
Spanish text: á é í ó ú ñ
Use it from .Rmd code: it comes out right...
print("á é í ó ú ñ")
## [1] "á é í ó ú ñ"
read_chunk("TestSpanishText.R")
Spanish text comes out garbled here:
print("Called from .R file: á é à ó ú ñ")
## [1] "Called from .R file: á é à ó ú ñ"
# Workaround
my.text <- "á é à ó ú ñ"
Encoding(my.text) <- "UTF-8"
print(my.text)
## [1] "á é í ó ú ñ"
Thank you!
解决方案 Ideally I should have an encoding
argument in read_chunk()
, but since you were using UTF-8
, this probably works:
read_chunk(lines = readLines("TestSpanishText.R", encoding = "UTF-8"))
Please try this first. If it does not work, I will add an encoding
argument. Anyway, I'm sure this definitely works (it is just slightly longer):
con = file("TestSpanishText.R", encoding = "UTF-8")
read_chunk(con)
close(con)
这篇关于是否有knitr选项强制在包含的R文件中的UTF-8编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!