如何精确检查文件路径的非法字符?

How to precisely check for illegal characters of file paths?

我正在开发一个闪亮的应用程序,它为每个用户和他们的每个实验生成文件夹和子文件夹。

我希望确保用户和实验名称均不包含任何非法字符。

我用我知道的每个非法字符定义了一个字符向量,但是,有可能出现人为错误。 有更精确的方法吗?

dir <- "~/home/my_app_data"
usr <- "john"
exp <- "explosion`s"
path <- paste(dir, usr, exp, sep = "/")
illegal <- c(" ", ",", "`")

if (any(illegal %in% (strsplit(x = path, split = "") %>% unlist))) {
  stop( "Illegal characters used")
} else {
  dir.create(path, recursive = T)
}
dir <- "~/home/my_app_data"
usr <- "john"
exp <- "explosion`s"
path <- paste(dir, usr, exp, sep = "/")

应该可以防止大多数错误:

if(!(identical(gsub(
  "[^[:alnum:]]+",
  "_",
  iconv(exp, from = "ascii", "utf-8")
), exp))) {
  stop("Illegal characters used")
} else {
  dir.create(path, recursive = TRUE)
}

使用greplpattern="\W" 查找不包括下划线的非单词字符 "_".

FUN <- function(x) {
  if (grepl("\W", x)) stop(sprintf("Illegal characters used in '%s'", x)) else x
}

FUN(usr)
# [1] "john"

FUN(exp)
# Error in FUN(exp) : Illegal characters used in 'explosion`s'

lapply(c(usr, exp), FUN)
# Error in FUN(X[[i]], ...) : Illegal characters used in 'explosion`s' 

FUN("john123")
# [1] "john123"

FUN("john_123")
# [1] "john_123"

(您当然想定义自定义 else 条件。)