[英]Delete the filename from the list when faced an error
I have this code我有这个代码
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")
})
}
While everything else seem to be working, filenames[names(filenames) != f] is unable to remove the errored file name from the list 'filenames'.虽然其他一切似乎都在工作,但 filenames[names(filenames) != f] 无法从列表 'filenames' 中删除错误的文件名。 I even tried the number method but all in vein .. could it be because it's inside the error function ?
我什至尝试了数字方法,但一切都在血管中..可能是因为它在错误函数内?
Appreciate if you can suggest a way out please.感谢您是否可以建议出路。
You could create a vector to store the result and turn it to "success"
if the file correctly reads and will remain blank in case of error.您可以创建一个向量来存储结果并在文件正确读取时将其转换为
"success"
,并且在出现错误时将保持空白。
file_read <- character(length(filenames))
for (i in seq_along(filenames)) {
tryCatch(
{
nverr <- read_excel(paste(layout_path,filenames[i],sep=''), sheet = 1, col_names = FALSE, range = cell_cols("A:A"))
file_read[i] <- 'success'
},
error=function(err) {
cat("File doesn't appear to open:", f, conditionMessage(err), "\n")
})
}
You can then remove the unsuccessful files.然后,您可以删除不成功的文件。
newfilenames <- filenames[file_read != 'success']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.