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.