0

I wrote a script where I load some packages, a specific font (via extrafont), do some data wrangling, plot a graph via ggplot() and save the graph via ggsave() with device "cairo_pdf".

Everything works fine except a tiny difference by using either R-Studio or R to source the script. In R-Studio it works with no problems. In R it also runs without problems, but the PDF-File doesn't show German Umlauts (ä,ö,ü). Instead "für" it shows "für" for example.

This only happens when sourcing the script in R. When I copy & paste the contents of the script to the Console in R, the pdf contains the Umlauts as it should. Also sourcing in R-Studio is showing the Umlauts.

I'm working on Windows 10 and using R 4.0.3. Not sure if you need an MWE since this doesn't seem to be a code related problem?

thuettel
  • 165
  • 1
  • 11

1 Answers1

1

The handling of Unicode by source on Windows is unfortunately broken.

You must use parse + eval instead:

eval(parse('filename.r', encoding = 'UTF-8'))
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • The problem I have is I don't know how the receiver of the script will execute it. It is part of an exam in a study course. I was not sourcing manually, I was using File -> Read R Code ... because I thought this might be what the Prof will be doing. Do you know of any other solution to this? – thuettel Feb 10 '21 at 20:16
  • 1
    @thuettel Short of [replacing the characters with escapes](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html) in the file I have no solution. E.g. `\u00E4` instead of `ä`. You can look up the values in the “Character Map” application or online. – Konrad Rudolph Feb 10 '21 at 21:05
  • Could you maybe show the full code including ggsave to show where eval(parse('filename.r', encoding = 'UTF-8')) is used? Many thanks in advance! – user9898927 Jun 16 '21 at 15:31
  • @user9898927 `ggsave` would be used *inside* the script (here: `filename.r`). `eval(parse(…))` would be used instead of `source`. If you’re not using `source`, this solution isn’t relevant for you. – Konrad Rudolph Jun 16 '21 at 16:10