问题描述
环境:
R v. 2.15.1 在 Mac OS 10.8.2 平台 x86_64-apple-darwin9.8.0/x86_64(64 位)上,RStudio IDE 设置为使用 UTF-8 作为其默认编码.操作系统也使用 UTF-8.
R v. 2.15.1 on Mac OS 10.8.2, platform x86_64-apple-darwin9.8.0/x86_64 (64-bit), with RStudio IDE which is set to use UTF-8 as its default encoding. The OS also uses UTF-8.
> Sys.getlocale(category = "LC_ALL")
[1] "sk_SK.UTF-8/sk_SK.UTF-8/sk_SK.UTF-8/C/sk_SK.UTF-8/sk_SK.UTF-8"
目标:
从 R HTML (.Rhtml) 文件生成 HTML 文件,其中包含带有扩展拉丁字符(例如 š 或 č)的绘图.
Generate an HTML file from the R HTML (.Rhtml) file, containing a plot with extended latin characters, such as š or č.
问题:
当我点击Knit HTML时,输出如下所示:
When I click on Knit HTML, the output looks like this:
plot(1:2, main = "šč")
## Warning: conversion failure on 'šč' in 'mbcsToSbcs': dot substituted for
##
## Warning: conversion failure on 'šč' in 'mbcsToSbcs': dot substituted for
##
## Warning: conversion failure on 'šč' in 'mbcsToSbcs': dot substituted for
##
## Warning: conversion failure on 'šč' in 'mbcsToSbcs': dot substituted for
## <8d>
**Plot with correct characters despite the warnings.**
问题:
导致问题的原因以及如何解决?我怎样才能至少摆脱结果文件中显示的警告?
What causes the problem and how to solve it? How can I at least get rid of the warnings which do show in the resulting file?
绝望的注解:
过去一两个小时我一直在寻找解决方案,发现了许多类似的情况并尝试了许多不同的潜在解决方案(许多与 PDF 输出有关,如果我只使用 Sweave 会以同样的方式出现),现在我真的没有希望了.
I have been searching for a solution for the past hour or two, found many similar cases and tried many different potential solutions (many related to PDF output, which is cocked up the same way if I use just Sweave), and now I am literally hopeless.
2012 年 11 月 9 日
@metasequoia 建议的使用 Encoding()
的解决方案确实有效,但考虑到需要打印代码,最好没有该功能,我更喜欢@nograpes 使用该功能提供的解决方案pdf.options()
.
The solution using Encoding()
suggested by @metasequoia does work, but considering the need to print the code as well, preferably without that function, I prefer the solution provided by @nograpes using the function pdf.options()
.
有趣的是,虽然
<!--begin.rcode
pdf.options(encoding='ISOLatin2.enc')
plot(cars, main="Ťažký")
end.rcode-->
产生相同的警告,
<!--begin.rcode
pdf.options(encoding='ISOLatin2.enc')
end.rcode-->
<!--begin.rcode
plot(cars, main="Ťažký")
end.rcode-->
按预期工作.这是为什么?我认为在 R 中运行命令时,时间顺序才是最重要的.
works as expected. Why is that? I thought that chronology is all that matters when running commands in R.
所以,我的目的的明确解决方案是把
So, the definite solution for my purposes is to put
<!--begin.rcode echo="FALSE"
pdf.options(encoding='ISOLatin2.enc')
end.rcode-->
在我的每个代码的开头.
in the beginning of each of my codes.
推荐答案
@metasequoia 的回答有效,但我想补充几点.如果您将 PDF 选项设置为不同的编码,则不需要将所有输出文本都包装在 Encoding
中.在点击 Knit HTML 之前运行这个:
The answer from @metasequoia works, but I wanted to add a few points. If you set the PDF options to a different encoding you won't need to wrap all your output text in Encoding
. Run this before clicking Knit HTML:
pdf.options(encoding='ISOLatin2.enc')
Ripley 在此处的帖子,它可能会引起人们的兴趣.值得注意的是,此错误在 Windows 上不会以相同的方式发生,因为编码的处理方式完全不同.
Ripley talks about encoding issues, especially as the relate to PDFs, in a post here, and it may be of interest. Notably, this error will not occur in the same way on Windows, because encoding is handled in a completely different way.
其他语言可能需要不同的编码文件,但这似乎适用于斯洛伐克语.
A different encoding file may be needed for other languages, but this seems to work for Slovak.
这篇关于Rhtml:警告:“<var>"上的转换失败在“mbcsToSbcs"中:点替换为<var>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!