0

I have 3 files.

library(openxlsx)
file1 <- '/home/mycomp/file1.xlsx'
file2 <- '/home/mycomp/file2.xlsx'
file3 <- '/home/mycomp/file3.xlsx'

fil <- c(file1, file2,file3)

Each file has multiple sheets. I am trying to print the sheeetNames from each file using a forloop.

for (i in fil) {
  sheets <- openxlsx:::getSheetNames(fil[i])
  for (s in sheets) {
    print(s)
  }
}

I am getting the following error:

Error in getSheetNames(fil[i]) : file does not exist.

However when I run the file individually, I get the sheet names printed.

for (i in fil) {
  sheets <- openxlsx:::getSheetNames(file1)
  for (s in sheets) {
    print(s)
  }
}

In this case I get the sheet names printed 3 times as the fil contains 3 files. Why does my forloop prints the error in earlier case.

Apricot
  • 2,925
  • 5
  • 42
  • 88

1 Answers1

0

Please use i rather than fil[i].

 for (i in fil) {
        sheets <- openxlsx:::getSheetNames(i)
        for (s in sheets) {
            print(s)
        }
    }
MSW Data
  • 441
  • 3
  • 8