It works perfectly when I run it line by line in the script but when I try to save as a user library with "Source on save" activated or I try to "Run all" with Ctrl+Alt+R (that makes a source of the script) it fails with this message:
> source('99_www.R', encoding = 'UTF-8')
Error in source("99_www.R", encoding = "UTF-8") :
99_www.R:9:53: inesperado INCOMPLETE_STRING
8: unidades == "-" ~ "",
9: str_detect(string = unidades, pattern = regex("
^
Además: Warning message:
In readLines(file, warn = FALSE) :
entrada inválida encontrada en la conexión de entrada '99_www.R'
What I should change in the code to be able to run the script?
I can't replicate your issue, I have copied your code and source it without any problems, can you give more details about your setup? (Operating system, locale configuration, etc.)
The problem arise because I include inside the regex this characters that are in Russian "КГ" and the source is done by RStudio with UTF-8 as encoding by default. Same happens in console with this code:
source('99_www.R', encoding = 'UTF-8')
If I delete this characters or I source the file without the encoding in the console, it works perfectly.
But when I source a file with this function in a script and it is executed from the editor, even if I source the file without encoding, it does not work
source('99_www.R')
Summarizing:
The origin of the problem is that there are characters in Russian in the regex
The script works in the console and when it is executed as a Job. It works also from an independent R session.
The script does not work when it is sourced with encoding="UTF-8" or through the Run all command in the editor or saved with Source on save active
Even without explicitly use the encoding parameter, it fails when another script source this file and is runned from the editor
I have updated to the last version of RStudio 1.4.1071 and the problem continues.
Is there any way to change the encoding of how the scripts are sourced by default in the editor of RStudio?
I can replicate your issue on Windows if I save the file with UTF-8 encoding (Windows is not UTF-8 friendly) but if I change to the system default (ISO-8859-1), I can source without problems.