Handle encoding correctly with knitr>=1.0 (#8504)

Knitr now has a parameter to indicate the encoding of the file. With this patch,it is used when possible (from Yihui Xie).
This commit is contained in:
Jean-Marc Lasgouttes 2013-01-21 09:48:28 +01:00
parent 98a8c30dfa
commit c48bdf845c

View File

@ -21,12 +21,8 @@
## $$e encoding (e.g. 'UTF-8') ## $$e encoding (e.g. 'UTF-8')
library(knitr) library(knitr)
.cmdargs = commandArgs(TRUE) .cmdargs = commandArgs(TRUE)
.orig.enc = getOption("encoding")
options(encoding = .cmdargs[3])
## the working directory is the same with the original .lyx file; you ## the working directory is the same with the original .lyx file; you
## can put your data files there and functions like read.table() can ## can put your data files there and functions like read.table() can
## work correctly without specifying the full path ## work correctly without specifying the full path
@ -34,4 +30,11 @@ setwd(.cmdargs[4])
opts_knit$set(root.dir = getwd()) opts_knit$set(root.dir = getwd())
## run knit() to get .tex or .R ## run knit() to get .tex or .R
if (is.null(formals(knit)$encoding)) {
.orig.enc = getOption("encoding")
options(encoding = .cmdargs[3])
knit(.cmdargs[1], output = .cmdargs[2], tangle = 'tangle' %in% .cmdargs) knit(.cmdargs[1], output = .cmdargs[2], tangle = 'tangle' %in% .cmdargs)
} else {
knit(.cmdargs[1], output = .cmdargs[2], encoding = .cmdargs[3],
tangle = 'tangle' %in% .cmdargs)
}