I am new to R and trying to write some code that will loop over all the files in my a folder, and then pull in all the data associated with a certain tab. However, this tab may not be present in all of the files I have stored in this folder. To trouble shoot this, I am using a Try-Catch function, but am still running into issues.
What else do I need to do so I just loop over the data if the tab is not present, and not load it in?
This is what I have tried:
for (i in 1:nrow(filesinfolderfull_list)){
print(filesinfolder_list[i])
i_ddolv_temp <- tryCatch (
{ read_excel(filesinfolderfull_list$datafiles[i], sheet="Display-OLV Reporting",col_names=TRUE,skip=4)},
error = function(e){print("skip")}
)
templateDDOLV_df<- bind_rows(templateDDOLV_df,i_ddolv_temp)
}
答案 0 :(得分:0)
我从您的解释中得知,有些专家没有"Display-OLV Reporting"
工作表。为什么不先使用excel_sheets
查找,然后提取TRUE
。像这样:
for (i in 1:nrow(filesinfolderfull_list)){
print(filesinfolder_list[i])
if ("Display-OLV Reporting"%in%excel_sheets(read_excel(filesinfolderfull_list$datafiles[i]){
i_ddolv_temp <- read_excel(filesinfolderfull_list$datafiles[i], sheet="Display-OLV Reporting",col_names=TRUE,skip=4)
templateDDOLV_df<- bind_rows(templateDDOLV_df,i_ddolv_temp)
}
}
您可以使用变量不两次读取每个excel