如何在R的书形标题中使用UTF-8字符 [英] How to use UTF-8 characters in dygraph title in R
问题描述
使用Rstudio [Windows8],当我使用dygraph函数绘制时间序列时,尝试在主标题中使用UTF-8字符时遇到问题.
Using Rstudio [Windows8], when I use the dygraph function to plot a time series, I have a problem when trying to use UTF-8 characters in the main title.
library(dygraphs)
dygraph(AirPassengers, main = "Título")
这将产生一个标题:"T?tulo"
This results in a title: "T?tulo"
我尝试将Título"转换为utf-8编码,但不起作用.
I have tried to convert "Título" to the utf-8 enconding, but it doesn't work.
推荐答案
您需要确保您的语言环境设置支持您要使用的字符,并且文件已使用正确的编码保存.另存为UTF-8对我有用.
You need to make sure your locale settings support the character that you want to use, and that the file is saved with the right encoding. Saving as UTF-8 worked for me.
我能够在Windows 7中复制您的情况,并尝试了很多方法.嵌入在Rmarkdown中,这是一个最小的工作示例.
I was able to replicate your situation in Windows 7 and tried a bunch of things. Embedded in Rmarkdown, here is a minimal working example.
```{r}
Sys.setlocale("LC_ALL","German")
#note that windows locale names are different from unix & mac, usually
#the name of nationality works here.
#also works with "Faroese", "Hungarian", and others who have this letter.
#the locale has to be set in a preceding block to take effect.
```
```{r}
Encoding("Título")
library(dygraphs)
dygraph(AirPassengers, main = "Título")
```
您可以尝试使用Encoding()
赋予标题的编码.诸如法罗语,匈牙利语和德语之类的语言将Título"编码为拉丁语或未知语,这两种语言对于dygraph的javascript似乎都不会造成问题. UTF-8将其写为<U+00ED>
,这对于javascript以及某些但不是所有其他功能来说都是一个问题.在具有匹配的语言环境的情况下,按照@Michele的建议转换为utf-8的结果相同.
You can try out the encoding given to the title to have with Encoding()
. Languages like Faroese, Hungarian, and German encode "Título" as latin or unknown, both of which seem to cause no problems for dygraph's javascript. UTF-8 wrote it as <U+00ED>
which was a problem for the javascript, as well as some, but not all other functions. With a matching locale, converting to utf-8 as @Michele recommended has the same result.
此外,如果您在很多地方都没有标题,则可以在创建的html/javascript文件中手动查找和替换标题.转换时会出现问题,但是如果已经制作了文件,则可以成功更改title变量.该字母在Rstudio查看器"输出中仍然带有问号,但是我建议定期制作整个javascript文件,因为我已经在查看器窗口中看到其他功能出现了问题.
Also, if you don't have the title in many places, it is possible to just manually find and replace the title in the html/javascript file that is made. The problem occurs on conversion, but if the file is already made, the title variable can be successfully changed. The letter still has a question mark in Rstudio "Viewer" output, but I recommend making the entire file for javascript regularly, as I've seen other functions malfunction in the viewer window.
这篇关于如何在R的书形标题中使用UTF-8字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!