Environment:
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"
Objective:
Generate an HTML file from the R HTML (.Rhtml) file, containing a plot with extended latin characters, such as š or č.
Problem:
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.**
Question:
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?
Hopeless note:
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.
Edit on 9 November 2012:
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()
.
It is interesting, though, that while
<!--begin.rcode
pdf.options(encoding='ISOLatin2.enc')
plot(cars, main="Ťažký")
end.rcode-->
produces the same warnings,
<!--begin.rcode
pdf.options(encoding='ISOLatin2.enc')
end.rcode-->
<!--begin.rcode
plot(cars, main="Ťažký")
end.rcode-->
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.
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 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.
Just to explain the solution you found:
<!--begin.rcode
pdf.options(encoding='ISOLatin2.enc')
end.rcode-->
<!--begin.rcode
plot(cars, main="Ťažký")
end.rcode-->
This works whereas it does not work when you put the two lines in the same chunk because for each code chunk, knitr
opens a new graphical device to record plots (by default it is a PDF device). It is too late to set pdf.options()
because the device has been opened with the default encoding when you put pdf.options()
and plot()
in the same chunk.
In the working solution, when the PDF device opens for the second chunk, it inherits the encoding from the setting in the previous chunk; that is how it produces the characters correctly.
If you do not want to set this encoding option in each of your Rhtml file, you can put it in ~/.Rprofile
so it affects all your PDF devices. Or you define your own function to knit Rhtml files, e.g.
knit2 = function(...) {
pdf.options(encoding='ISOLatin2.enc')
knitr::knit(...)
}
Then knit2('yourfile.Rhtml')
.
Using @nograpes reproducible approximation of an example (R 2.15.1 on Mac OSX):
pdf()
plot(1:2,main="šč")
dev.off()
I was able to replicate the OP's error code. Wrapping "šč" with Encoding()
eliminated the warning messages.
pdf()
plot(1:2,main=Encoding("šč"))
dev.off()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With