3

I try to deal with encoding UTF-8 my R package. My R version is 3.4.4 on Windows.

My package is composed of some functions with console printing and graph who needed encoding UTF 8 (french).

I try to add this line in my R Script (at the beginning of script containing my function and in my function) but the printing is like this "Répartition de la différence"

Sys.setlocale("LC_CTYPE","french")
options(encoding = "UTF-8")

In another script, after load my package, I also add this few line but I have the same encoding problem ...

Any ideas ?

Cox Tox
  • 661
  • 3
  • 8
  • 22
  • What's the expected output? – Oliver Frost May 17 '18 at 10:04
  • if A is your column of names, then use Encoding(A) <- "UTF-8". – Prany May 17 '18 at 10:07
  • @OliverFrost: I would like to have in my outputs strings encoding like : **"Répartition de la différence"** and not "Répartition de la différence". – Cox Tox May 17 '18 at 12:06
  • @Prany : This is not a dataframe. Encoding refers strings print in console or text (title/xlab/ylab) in graphs. – Cox Tox May 17 '18 at 12:06
  • Hi. Have you come across a solution to this issue? I also have packages which are heavy on non-ascii characters. Some rare times the installation fails and the letters get scrambled but then I try other ways to install and things work out fine. But would be good to know what the variables are. – s_baldur Oct 13 '21 at 09:53

1 Answers1

3

You can add a line specifying Encoding: UTF-8 in your DESCRIPTION file.

See https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Character-encoding-issues

If the DESCRIPTION file is not entirely in ASCII it should contain an ‘Encoding’ field specifying an encoding. This is used as the encoding of the DESCRIPTION file itself and of the R and NAMESPACE files, and as the default encoding of .Rd files. The examples are assumed to be in this encoding when running R CMD check, and it is used for the encoding of the CITATION file. Only encoding names latin1, latin2 and UTF-8 are known to be portable. (Do not specify an encoding unless one is actually needed: doing so makes the package less portable. If a package has a specified encoding, you should run R CMD build etc in a locale using that encoding.)

Please let me know if it solves your issue.

chinsoon12
  • 25,005
  • 4
  • 25
  • 35
  • Thank's ! When I add this line that's fix the problem for print encoding in console but not for the encoding in graph (title/xlal/ylab). I don't understand how to fix encoding (in namespace) with your link. – Cox Tox May 23 '18 at 12:12
  • What function do you use to plot? – chinsoon12 May 23 '18 at 12:44
  • I use ggplot function : `g<- ggplot(df,aes=(x,y)) + labs(title="Encoding UTF-8",y="Fréq") print(g)` – Cox Tox May 23 '18 at 12:52
  • you might need to escape those char? see https://stackoverflow.com/questions/13819972/utf-8-in-ggplot-axis-labels and https://stackoverflow.com/a/38202460/1989480. i.e. need to add `options(encoding = "UTF-8")` to your `.onAttach` and `.onLoad` functions – chinsoon12 May 23 '18 at 13:21
  • there is no way to force encoding in title or ylab ? I also try to add `Sys.setlocale("LC_CTYPE","french")` but it doesn't work .. – Cox Tox May 23 '18 at 13:44