微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 R 跳过列表错误

如何解决使用 R 跳过列表错误

我有一个'for',它试图加载多个文件,但是,当打开这些文件中的任何一个时,它会得到以下错误

Error: Evaluation error: zip file 'V:/Planejamento/2021/Programação/04. Abr/Balun/~$Schedule Balun 0804.xlsx' cannot be opened.

有什么办法可以让列表中的搜索和记录继续只忽略那些打开的吗?

代码

for(i in i:length(arquivo_caracter)){
  master[[i]] <- data.frame(read_excel(arquivo_caracter[i],sheet = "Master",skip = 0,.name_repair = "minimal"))
  dados[[i]] <- data.frame(read_excel(arquivo_caracter[i],sheet = "Dados",skip = 1,.name_repair = "minimal"))
}

解决方法

如评论中所建议,使用 tryCatch

arquivo_caracter <- list("test1.xls","test2.xls")

for(i in 1:length(arquivo_caracter)){
  tryCatch({
   master[[i]] <- data.frame(read_excel(arquivo_caracter[i],sheet = "Master",skip = 0,.name_repair = "minimal"))
   dados[[i]] <- data.frame(read_excel(arquivo_caracter[i],sheet = "Dados",skip = 1,.name_repair = "minimal"))},error = function(e) {warning(paste("Couldn't open",arquivo_caracter[i]))}
  )
}

#Warning messages:
#1: In value[[3L]](cond) : Couldn't open test1.xls
#2: In value[[3L]](cond) : Couldn't open test2.xls

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。