0

Using the writexl package I can write a data.frame into an xlsx file, while keeping special characters in the xlsx file, because the data is saved in UTF-8 encoding (on Mac). This works when I run my R script from the R console.

enter image description here

However, when I schedule a cronjob to run the exact same R-script, the xlsx file is saved using a different encoding so that special characters are not shown. Is there a way to change the encoding in a cronjob.

enter image description here

I could not find any documentation on how to add an argument related to the encoding using the cronR package (https://github.com/bnosac/cronR) or writexl package nor change the encoding from within the R script using any the following lines:

options(encoding = "UTF-8")

or by changing the encoding of the character columns into UTF-8:

data_table <- data_table %>% mutate_if(is.character,
                                       list(~enc2utf8(.)))

before writing the dataframe to xlsx

writexl::write_xlsx(data_table, "filename.xlsx")

Does anyone know how the encoding can be changed to UTF-8 while running a cronjob so that special characters are visible in the saved xlsx file?

1 Answers1

0

I came across a solution by TDaw, which fixes the problem:

It turns out the settings cannot be specified in R script as Cron over rides them. The solution was to do this in cron directly through Terminal. Easiest if you are using cronR to edit the crontab once you have added your cron jobs.

In terminal type crontab -e to get into the crontab. The type in i this will allow you to insert or edit the crontab. At the top of the file I entered:

LANGUAGE=gb
LC_CTYPE=en_GB.UTF-8
PYTHONIOENCODING=utf8

This stops cron using POSIX as its language. The you hit esc and the :wq to save and exit. If you edit your cron jobs in Rstudio/CronR they will go above the edit and will not use those settings so the crontab will have to edited accordingly.