Case_when error on source

Hi,
I have the following code using case_when:

library(tidyverse)

estandarizar_unidades <- function(unidades) {
  aux <- tibble(unidades = unidades)
  aux <- aux %>% 
    mutate(Unit = case_when(
      unidades == "-" ~ "",
      str_detect(string = unidades, pattern = regex("КГ|62|NOS|kg|Kilo", ignore_case = TRUE)) ~ "Kg",
      str_detect(string = unidades, pattern = regex("^TON|^MTS", ignore_case = TRUE)) ~ "MT",
      str_detect(string = unidades, pattern = regex("^GRAM|GRM|GMS", ignore_case = TRUE)) ~ "gr",
      str_detect(string = unidades, pattern = regex("UNI|PIECE|PCS", ignore_case = TRUE)) ~ "Unit",
      str_detect(string = unidades, pattern = regex("BAG|BOLSA", ignore_case = TRUE)) ~ "Bag",
      str_detect(string = unidades, pattern = regex("BOX|CAJ", ignore_case = TRUE)) ~ "Box",
      str_detect(string = unidades, pattern = regex("LIBRAS|LBS", ignore_case = TRUE)) ~ "Pounds",
      TRUE ~ "Other"))
}

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?

Thanks

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.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

This is my session info.

- Session info ---------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 4.0.3 (2020-10-10)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  Spanish_Spain.1252          
 ctype    Spanish_Spain.1252          
 tz       Europe/Paris                
 date     2020-12-02                  

- Packages -------------------------------------------------------------------------------------------------------------------------------
 package     * version date       lib source        
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.2)
 backports     1.1.10  2020-09-15 [1] CRAN (R 4.0.2)
 callr         3.5.0   2020-10-08 [1] CRAN (R 4.0.3)
 cli           2.1.0   2020-10-12 [1] CRAN (R 4.0.3)
 crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.2)
 desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.2)
 devtools      2.3.2   2020-09-18 [1] CRAN (R 4.0.3)
 digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.2)
 ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.2)
 fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.2)
 fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
 glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
 magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.2)
 memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.3)
 pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)
 pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.2)
 prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.2)
 processx      3.4.4   2020-09-03 [1] CRAN (R 4.0.2)
 ps            1.4.0   2020-10-07 [1] CRAN (R 4.0.2)
 R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.2)
 remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.2)
 rlang         0.4.7   2020-07-09 [1] CRAN (R 4.0.2)
 rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.2)
 rstudioapi    0.11    2020-02-07 [1] CRAN (R 4.0.2)
 sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.3)
 testthat      2.3.2   2020-03-02 [1] CRAN (R 4.0.2)
 usethis       1.6.3   2020-09-17 [1] CRAN (R 4.0.3)
 withr         2.3.0   2020-09-22 [1] CRAN (R 4.0.2)

[1] C:/Users/avidal/Documents/R/win-library/4.0
[2] C:/Program Files/R/R-4.0.3/library