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