Home » excel » r – How to ignore hidden data when importing from Excel

r – How to ignore hidden data when importing from Excel

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a collection of excel files which I am importing into R.

The files contain hidden data which I would like to disregard — eg, simply not importing it, or importing it with a flag indicating it was hidden so that I can then drop it.

The files contain two types of hidden data:

  • Complete sheets are hidden
  • Specific Rows within a sheet are hidden.

Is there a way to identify when data in excel is hidden?

Right now I am using the gdata package, but am happy to use XLConnect or other package

Sample Code:

library(gdata)
xlsfile <- "test.xls"

# grab all the sheet names. 
# This is giving me both hidden & non-hidden sheets.  I would like only the latter
sheets <- sheetNames(xlsfile)

# read in the xls file, by sheet 
xlData <- 
  lapply(sheets, function(s) 
      read.xls(xlsfile, sheet=s, stringsAsFactors = FALSE))

if needed, I can create a dummy xls file and post it.

How to&Answers:

XLConnect has a nice function called isSheetHidden which does what you want. Assuming Sheet2 is hidden:

library(XLConnect)
xlsfile <- "Book1.xls"
wb <- loadWorkbook(xlsfile, create = TRUE)
isSheetHidden(wb, "Sheet1") # FALSE
isSheetHidden(wb, "Sheet2") # TRUE

In gdata you would have to write your own function that calls the underlying perl package to access the sheet property, but it is possible.