Search code examples
rexcelimportopenxlsx2

Bug importing with openxlsx2 in R?


I find openxlsx2 a very interesting package, it's very useful how you can import a workbook keeping styles.

I just wondering if it is a bug or I'm doing something wrong for the very simple task of importing an excel workbook with openxlsx2::wb_load. I just want to import first sheet as in openxlsx::read.xlsx(..., sheet = 1).

According to openxlsx2 documentation sheet: if this is applied, only the selected sheet will be loaded. This can be a numeric, a string or NULL

library(openxlsx2)

> openxlsx2::wb_load("example.xlsx")
A Workbook object.
 
Worksheets:
 Sheets: ES FR UK 
 Write order: 1, 2, 3

I almost get same result if I do:

> openxlsx2::wb_load("example.xlsx", sheet = 1)
A Workbook object.
 
Worksheets:
 Sheets: ES FR UK 
 Write order: 1, 2, 3

It still imports all sheets, though data in sheets 2 and 3 has been removed. I want to import only sheet 1.

I have also tried openxlsx2::wb_load("example.xlsx", sheet = "ES") with no result.


Solution

  • Thanks for your interest in openxlsx2. This works as expected. Only a single sheet is imported, but the other sheets remain in the workbook, because on load we do not mess with any of the other internal files of the workbook. This argument was intended for use in wb_to_df()/read_xlsx() when used with the sheet argument when it's not required to load unwanted worksheets.

    You can remove unwanted worksheets with openxlsx2::wb_remove_worksheet().

    If you have a suggestion how to improve the documentation please open a pull request or raise an issue on github. And yes, this would have been a nice question for our githubs discussion section.