2013-03-29 9 views
7

Sto usando Windows 7, R2.15.3 e RStudio 0.97.320 con knitr knitr_1.1.6 (scaricati dopo Yihui fissato il 'Encoding: knitr e bambino files' questione il 12 marzo)Esiste un'opzione knitr per forzare la codifica UTF-8 nei file R inclusi?

> 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 

ho il mio codice R in un file come questo:

## @knitr RunMyCode 
print('Called from .R file: á é í ó ú ñ') 

# Workaround 
my.text <- 'á é í ó ú ñ' 
Encoding(my.text) <- "UTF-8" 
print(my.text) 

io lo chiamo da un file Rmd come questa:

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')} 
``` 

il mio problema è con i caratteri digitati spagnoli nel file .R (che è codificato in UTF-8 in RStudio). Questi caratteri sono OK se digitati in file Rmd (entrambi i file genitore e figlio funzionano bene), ma non nei file R. Come potete vedere qui sotto, Encoding() fornisce una soluzione alternativa, ma mi chiedo se c'è un altro modo, come un'opzione globale? Se uso Encoding(), ottengo il problema inverso nella console RStudio ...

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] "á é í ó ú ñ" 

Grazie!

risposta

5

Idealmente mi dovrebbe avere un argomento in encodingread_chunk(), ma dal momento che si stava utilizzando UTF-8, questo probabilmente funziona:

read_chunk(lines = readLines("TestSpanishText.R", encoding = "UTF-8")) 

Si prega di provare questo primo. Se non funziona, aggiungerò un argomento encoding. Ad ogni modo, sono sicuro che funziona sicuramente (è solo leggermente più lungo):

con = file("TestSpanishText.R", encoding = "UTF-8") 
read_chunk(con) 
close(con) 
+0

La prima soluzione funziona alla grande !! Grazie Yihui !! – ap53

+0

Ho provato questo approccio ('con = file (" TestSpanishText.R ", encoding =" UTF-8 "); read_chunk (con); close (con)') per il mio problema: https://stackoverflow.com/questions/48307007/printing-utf-8-russian-characters-in-r-rmd-knitr, ma non è riuscito a farlo funzionare. ??? – IVIM

Problemi correlati