【发布时间】:2020-08-26 04:34:59
【问题描述】:
我有这个代码
library(plyr)
library(readxl)
library(XLConnect)
#Set the Path
layout_path <- "/rdrive/my_test/"
output_path <- "/rdrive/my_test/"
filenames <- list.files(path = layout_path , pattern="*xls", ignore.case=TRUE)
filecount <- length(list.files(path = layout_path, pattern="*xls", ignore.case=TRUE))
err_fs =
for (f in filenames) {
tryCatch(
{
nverr <- read_excel(paste(layout_path,f,sep=''), sheet = 1, col_names = FALSE, range = cell_cols("A:A"))
},
error=function(err) {
filenames[names(filenames) != f]
cat("File doesn't appear to open:", f, conditionMessage(err), "\n")
})
}
虽然其他一切似乎都在工作,但 filenames[names(filenames) != f] 无法从“文件名”列表中删除错误的文件名。 我什至尝试了数字方法,但一切都在脉络中..可能是因为它在错误函数内吗?
如果您能建议出路,请不胜感激。
【问题讨论】:
-
好吧,
filenames[names(filenames) != f]本身不做任何事情。您需要将该值保存在某处。但通常不建议在迭代向量时更新它。跟踪那些失败的列表会更好,然后在循环完成后将它们全部删除。
标签: r error-handling try-catch