Sweave不打印本地化字符

时间:2010-08-08 12:47:57

标签: pdf r sweave

我正试图通过Sweave在我的LaTeX文档中加入R中的一些图。

\SweaveOpts{eps = FALSE, pdf = TRUE, echo = FALSE, prefix = TRUE, prefix.string = data}

<<label = abundanca:barplot, fig = TRUE, include = FALSE, results = hide>>=
barplot(abund, xlab="Vzorčne postaje", ylab="Abundanca", main="", col="slategrey", names.arg=c("HM1", "HM2", "HM3", "HM4", "HM5", "HM6", "HM7", "HM8", "HM9", "HM10"))
@

Sweave中的pdf设备使用本机编码(在options("encoding")中设置),它不识别xlab中的本地字符(ščćž)(用两个点替换它们)。

我尝试将选项设置为适用于R:

的选项
options("encoding" = "CP1250.enc")

但是我收到了错误:

Error in file() : unsupported conversion from 'CP1250.enc' to ''

任何解决方案,解决方法......?

修改

通过

运行一个L3xa的Rnw
R CMD Sweave report.Rnw

不起作用。

通过Eclipse + StatET运行相同的文件

Sweave("report.Rnw")

但是,确实如此。

我的.Rnw文件和.pdf

1 个答案:

答案 0 :(得分:3)

这个并不像看起来那么简单。从技术上讲,这个问题是OS / locale / pdf writer / Sweave依赖(参见“R安装和管理”,第7章)。由于我正在运行GNU / Linux,因此这个“解决方案”不适用于Mac和Windows用户。而且只是为了让事情变得更复杂,GNU / Linux发行版会有所不同,所以如果Ubuntu的某些东西有效,那么它很可能在Arch Linux上不起作用。

我将使用mtcars数据集。让我们创建一些带有本地化字符的基本图形:

pdf("foo.pdf")
boxplot(mpg ~ cyl, data = mtcars, ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja")
dev.off()

(塞尔维亚语的速成课程:“Potrošnjagoriva”代表燃料消耗,“Broj cilindara”代表气缸数,“Dijagramraspršenja”代表散点图)

现在,我收到一堆警告:

Warning messages:
1: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") :
  conversion failure on 'Dijagram raspršenja' in 'mbcsToSbcs': dot substituted for <c5>
2: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") :
  conversion failure on 'Dijagram raspršenja' in 'mbcsToSbcs': dot substituted for <a1>
3: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") :
  conversion failure on 'Dijagram raspršenja' in 'mbcsToSbcs': dot substituted for <c5>
4: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") :
  conversion failure on 'Dijagram raspršenja' in 'mbcsToSbcs': dot substituted for <a1>
5: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") :
  conversion failure on 'Potrošnja goriva' in 'mbcsToSbcs': dot substituted for <c5>
6: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") :
  conversion failure on 'Potrošnja goriva' in 'mbcsToSbcs': dot substituted for <a1>
7: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") :
  conversion failure on 'Potrošnja goriva' in 'mbcsToSbcs': dot substituted for <c5>
8: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") :
  conversion failure on 'Potrošnja goriva' in 'mbcsToSbcs': dot substituted for <a1>

虽然options(encoding = "CP1250")没有做到这一点 - 我收到相同的警告,pdf.options(encoding = "CP1250")修补它,同样代表pdf(file = "foo.pdf", encoding = "CP1250")。所以,我会使用options(encoding = "native.enc")取回原来的编码,如前所述设置pdf.options并将事情做好。

有些用户只需设置pdf.options即可逃脱,并且不会遇到Sweave问题。因此,在开始绘图之前,应该在.Rnw文件中的某处插入这部分代码:

<<setOptions, echo = FALSE, results = hide>>==
pdf.options(encoding = "CP1250")
@

以后,只需:

<<plotTheFigure, echo = TRUE, fig = TRUE>>==
# I've set echo to TRUE intentionally, to prove my point here
boxplot(mpg ~ cyl, data = mtcars, ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja")
@

同样的情况代表ggplot2图。

你们中的一些人会得到正确的输出,但我没有!正如我之前所说,如果你正在运行Ubuntu,那么这很有可能会起作用,但就目前来说,我似乎无法让它活跃并在Arch中踢。

要保存击键,您可以下载Sweave file和/或PDF file(在Arch机器上执行)。如您所见,本地化字符在绘图函数中正确显示,但在Sweave中出现乱码。现在,如果我尝试将图形保存为PDF文件(没有Sweaving),我会得到correct output

所以,我已经解决了一些问题,但还有很多试错工作要做。

请在您的计算机上运行.Rnw文件,并给我一些反馈。为了方便起见,我创建了Rscript来收集我在这种情况下相关的系统信息(不是个人信息):这里是source,而这里是我的output