Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 'invalid regular expression '[:alpha:]+'' after migrating R and RStudio

Tags:

regex

r

I am having troubles running my code which was written under RStudio 1.3.959 after migrating to a new PC and installing RStudio 1.4.1717. The same error appears when running the code via base R (4.1.0). When using base R functions (grep, gregexpr, e.g. gregexpr("[:alpha:]+", "1234a")), there is no error message.

Code:

library(tidyverse)

data_files <- as.data.frame(list.files(data_folder)) 

data_files <- data_files %>%
  mutate(temp = data_files[,1]) %>%
  separate("temp",
           c("temp", "Trash"),
           sep = "\\.") %>%
  select(-"Trash") %>%
  separate("temp",
           c("run", "Trash"),
           sep = "[:alpha:]+", 
           remove = FALSE) %>%
  select(-"Trash") %>%
  separate("temp",
           c("Trash", "letters"),
           sep = "[:digit:]+") %>%
  select(-"Trash") %>%
  select("run", "letters") 

My data_folder contains csv files with name pattern (date-increment-letter.csv, e.g. 21021202a.csv)

Error message:

Error in gregexpr(pattern, x, perl = TRUE) : 
  invalid regular expression '[:alpha:]+'
In addition: Warning message:
In gregexpr(pattern, x, perl = TRUE) : PCRE pattern compilation error
    'POSIX named classes are supported only within a class'
    at '[:alpha:]+'

Reproducible example using dput:

data_files <- as.data.frame(list.files(icpms_folder))  
dput(head(data_files)) 

structure(list(list.files(icpms_folder) = c("21021202a.csv", 
                                            "21021202b.csv", 
                                            "21021202c.csv", 
                                            "21021203a.csv", 
                                            "21021203b.csv", 
                                            "21021203c.csv")), 
                 row.names = c(NA, 6L), class = "data.frame")

Could you point me what is missing in my fresh installation, please?

Thank you in advance!

like image 776
alexander baranov Avatar asked Mar 22 '26 04:03

alexander baranov


1 Answers

The answer to "why" is already in the error message: POSIX named classes are supported only within a class.

POSIX named classes are like [:digit:], [:alpha:], and so on.

By "class", the message author meant a character class, i.e. [...].

Put one inside of another:

sep = '[[:alpha:]]+'
like image 131
Ryszard Czech Avatar answered Mar 23 '26 18:03

Ryszard Czech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!