Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中读取带有空选项卡的excel文件?_R_Excel - Fatal编程技术网

如何在R中读取带有空选项卡的excel文件?

如何在R中读取带有空选项卡的excel文件?,r,excel,R,Excel,我正在R中导入excel文件。其中一些选项卡为空。R没有读那些标签。它给了我一个错误:跳过了所有数据 library(readxl) sheetnames = excel_sheets("Saudi Diapers.xlsx") k = length(sheetnames) i=1 for(i in 1:k) { abc = read_excel("Saudi Diapers.xlsx",i`,col_names = T) rownames(abc) = NULL tabna

我正在R中导入excel文件。其中一些选项卡为空。R没有读那些标签。它给了我一个错误:跳过了所有数据

library(readxl)
sheetnames = excel_sheets("Saudi Diapers.xlsx")
k = length(sheetnames)
i=1

for(i in 1:k) {

  abc = read_excel("Saudi Diapers.xlsx",i`,col_names = T)

  rownames(abc) = NULL

  tabname = paste(sheetnames[i], ".csv")

  write.csv(file=paste("Saudi Diapers",tabname,sep = "_"), x= abc, row.names = F,na="",sep = "")

}

您可以通过两种方式执行此操作:
1.更改函数
read\u excel
,使其不会出现错误,而是返回空数据帧 2.使用
tryCatch
捕捉错误并返回空数据帧。 我在这里选择了第二个选项。tryCatch函数实际上尝试执行一个表达式(在本例中为
read\u excel(…)
),如果该表达式生成错误,它将在错误条件下返回函数(在本例中为
data.frame(NULL)

我制作了一张excel表格,其中一张填好,两张空。

文件名为什么要读取空选项卡?或者你只是对那些不是空的感兴趣吗?我其实想在R。。。我不知道哪些标签是空的,哪些是非空的,但我想两者都读。
library(readxl)
sheetnames = excel_sheets(filename)

for(i in 1:length(sheetnames)) {

  tempdf = tryCatch(read_excel(filename,i,col_names = T), 
                 error = function(err) { data.frame(NULL)})

  write.csv(file=paste0("Saudi_Diapers_",sheetnames[i],".csv"), x= tempdf,
  row.names = F,na="")
      }