Non-English special characters in the book.

I am using knitr 1.1. in R 3.0.0 and in WinEdt (RWinEdt 2.0). I have problems with a knit that recognizes Swedish characters (ä, ö, å). This is not a problem with R; these characters are even recognized in file names, directory names, objects, etc. In Sweave, this is not a problem either.

I already have \usepackage[utf8]{inputenc} in my document, but knitr doesn't seem to be able to handle special characters. After starting knitr, I get the following message:

 Warning in remind_sweave(if (in.file) input) : It seems you are using the Sweave-specific syntax; you may need Sweave2knitr("deskriptiv 130409.Rnw") to convert it to knitr processing file: deskriptiv 130409.Rnw (*) NOTE: I saw chunk options "label=läser_in_data" please go to http://yihui.name/knitr/options (it is likely that you forgot to quote "character" options) Error in parse(text = str_c("alist(", quote_label(params), ")"), srcfile = NULL) : 1:15: unexpected input 1: alist(label=lä ^ Calls: knit ... parse_params -> withCallingHandlers -> eval -> parse Execution halted 

The specific label he complains about is label = läser. Changing the label is not enough, because knitr even complains if R objects use äåö.

I used Sweave2knitr () since the file was created for Sweave, but the result was no better: now all åå were converted to äpà ¥ ö, both in R fragments and in latex text, and knitr is still gives an error message.

Session Information:

 R version 3.0.0 (2013-04-03) Platform: i386-w64-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=Swedish_Sweden.1252 LC_CTYPE=Swedish_Sweden.1252 LC_MONETARY=Swedish_Sweden.1252 [4] LC_NUMERIC=C LC_TIME=Swedish_Sweden.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] knitr_1.1 loaded via a namespace (and not attached): [1] digest_0.6.3 evaluate_0.4.3 formatR_0.7 stringr_0.6.2 tools_3.0.0 

As I already mentioned, there are file names and objects with Swedish characters (since this was not a problem before), and the text should also be in Swedish.

Thank you for your help in making knitr work outside of English.

+7
source share
2 answers

I think you need to contact the R-Sweave mode maintainer in WinEdt if you use this mode to call knitr . The problem is that WinEdt must pass the file encoding to knit() if you are not using your OS's own encoding. You mentioned UTF-8 , but this is not native encoding for Windows, so you should not use \usepackage[utf8]{inputenc} unless you are sure that your file is encoded in UTF8.

There are several problems here, and they are unlikely to solve them with one answer.

The first problem is label=läser , which really should be label='läser' , i.e. you must specify all fragment labels (check other labels in the document as well); knitr tries to automatically indicate your labels when writing <<foo>>= (it refers to <<'foo'>>= ), but this does not work if you use <<label=foo>>= (you need to write <<label='foo'>>= explicitly). But this problem may not be significant here.

I think the real problem here is the file encoding (which is unpleasant under Windows). It seems you are using UTF-8 on a system that does not respect UTF-8 by default. In this case, you call knit('yourfile.Rnw', encoding = 'UTF-8') , i.e. knit() encoding to knit() . I do not use WinEdt, so I have no idea how to do this. You can hardcode the encoding in configurations , but this is not recommended.

Two suggestions:

  • Do not use UTF-8 under Windows; use your own encoding of your system ( Windows-1252 , I think);
  • or use RStudio instead of WinEdt, which can pass knitr encoding;

By the way, since Sweave2knitr() appeared, there should be other problems in your Rnw document. There are two ways to diagnose a problem:

  • if you are using UTF-8 , run Sweave2knitr('deskriptiv 130409.Rnw', encoding = 'UTF-8')
  • if you use your own encoding of your OS, just run Sweave2knitr('deskriptiv 130409.Rnw')

Please read the documentation if you have questions about the diagnostic information printed by Sweave2knitr() .

+3
source

R-Sweave calls knitr through the macro knitr.edt , which itself uses the code in knitrSweave.R to start knitting. The knit command in this later script is next to the top and reads res <- knit(filename) .

Following Yihui's suggestion, you can try replacing this command

 res <- knit(filename, encoding = 'UTF-8') 

The knitr.edt and knitrSweave.R should be in your %b\Contrib\R-Sweave , where %b is your winEdt user folder (something like "C:\Users\userA\AppData\Roaming\WinEdt Team\WinEdt 7" under Win 7).

Currently, I do not know how to pass the encoding as an argument to avoid this tough decision.

I suggest avoiding extended characters in file names, which can only be sources of problems. Personally, I never use such names.

+2
source

All Articles