I have a list of regexps which are used to produce some graphs. I'd like to save the graphs with it's corresponding regexp in the filename. Example:
re <- 'foo\\w{3}bar'
# ... produce a graph here and now need a filename
not_save <- paste0("pefix ", re, ".suffix")
But re
needs to be cleaned from everyting not allowed in filenames. I know, it's OS and filesystem related, but I think if it's a valid filename on Windows it's valid everywhere.
I can substitute bad characters with gsub()
:
not_save_enough <- gsub('[$^*|{}()/:]', '_', re, perl=TRUE)
But I don't know all bad chars and don't know how to replace \
and/or [
and ]
. Substitute all bad chars by _
would be sufficient. Unfortunally things like
gsub('\Q\\E', '_', "Numbers are \d", perl=TRUE)
arn't working even with perl = TRUE
and produce
Error: '\Q' is an unrecognized escape in character string starting "'/\Q"
Is there a function like make_string_save_to_use_it_as_filename()
?
How to substitude \
, [
and ]
and other regexp-meta-chars in strings?