Sweave 不打印本地化字符

发布于 2024-09-13 12:39:42 字数 1147 浏览 1 评论 0原文

我正在尝试通过 Sweave 将 R 中的一些绘图合并到我的 LaTeX 文档中。

\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 ''

有任何解决方案、解决方法...?

编辑

运行 aL3xa 的 Rnw

R CMD Sweave report.Rnw

不起作用。

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

Sweave("report.Rnw")

却可以。

我的 .Rnw 文件和 .pdf

I'm trying to incorporate some plots from R in my LaTeX document through Sweave.

\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"))
@

The pdf device in Sweave uses native encoding (as set in options("encoding")), which doesn't recognize local characters (ščćž) in xlab (replaces them with two dots).

I have tried setting the option to something that works in R:

options("encoding" = "CP1250.enc")

but I get an error:

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

Any solutions, workarounds...?

EDIT

Running aL3xa's Rnw through

R CMD Sweave report.Rnw

doesn't work.

Running the same file through Eclipse+StatET

Sweave("report.Rnw")

however, does.

My .Rnw file and a .pdf.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2024-09-20 12:39:42

这件事并不像看上去那么简单。从技术上讲,此问题与操作系统/语言环境/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šnja goriva”代表燃油消耗,“Broj cilindara”代表气缸数,“Dijagram rasprš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 文件,和/或 < a href="http://dl.dropbox.com/u/8566396/report.pdf" rel="nofollow noreferrer">PDF 文件(在 Arch 机器上执行)。正如您所看到的,本地化字符在绘图函数中显示正确,但在 Sweave 中却出现乱码。现在,如果我尝试将图形保存到 PDF 文件(不使用 Sweaving),我会得到 正确信息输出

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

请在您的计算机上运行 .Rnw 文件,并给我一些反馈。为了简化事情,我创建了 Rscript 来收集我认为与本例相关的系统信息(不是个人信息):此处的 来源,这是我的输出

This one is not as simple as it may seem. Technically, this problem is OS/locale/pdf writer/Sweave dependent (see "R Installation and Administration", chapter 7). Since I'm running GNU/Linux, this "solution" is not addressed to Mac and Windows users. And just to make things a bit more complicated, GNU/Linux distros differ, so there's a great chance that if something works on, say, Ubuntu, doesn't work on Arch Linux.

I'll be using mtcars dataset. Let's create some basic graph with localized characters:

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

(crash-course of Serbian language: "Potrošnja goriva" stands for fuel consumption, "Broj cilindara" stands for # of cylinders and "Dijagram raspršenja" is equivalent for scatterplot)

Now, I get bunch of warnings:

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>

While options(encoding = "CP1250") doesn't do the trick - I get the same warnings, pdf.options(encoding = "CP1250") mends it, and the same stands for pdf(file = "foo.pdf", encoding = "CP1250"). So, I'll get back my old encoding with options(encoding = "native.enc"), set pdf.options as previously stated and get things right.

Some users get away just by setting pdf.options, and get no problems with Sweave. So, you should insert this part of code somewhere in .Rnw file, before you start the plotting:

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

and later, just do:

<<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")
@

And the same scenario stands for ggplot2 graphs.

Some of you will get correct output, but I don't! And as I've said before, if you're running Ubuntu, there's a great chance that this will work, but for now, I can't seem to make it alive and kicking in Arch.

And to save your keystrokes, you can download Sweave file, and/or PDF file (executed on the Arch machine). As you can see, localized characters display correctly within the plot function, but get garbled within Sweave. Now, if I try to save graph to PDF file (without Sweaving), I get the correct output.

So, I've solved some issues, but there's a lot of trial-and-error job left to do.

Please run .Rnw file on your machine, and give me some feedback. To ease things up, I've created Rscript that collects your system info (not personal info) that I find relevant in this case: here' the source, and here's my output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文