Learn R Programming

openxlsx2 (version 0.6.1)

read_xlsx: Read from an Excel file or Workbook object

Description

Read data from an Excel file or Workbook object into a data.frame

Usage

read_xlsx(
  xlsxFile,
  sheet,
  startRow = 1,
  startCol = NULL,
  rowNames = FALSE,
  colNames = TRUE,
  skipEmptyRows = FALSE,
  skipEmptyCols = FALSE,
  rows = NULL,
  cols = NULL,
  detectDates = TRUE,
  namedRegion,
  na.strings = "#N/A",
  na.numbers = NA,
  check.names = FALSE,
  sep.names = ".",
  fillMergedCells = FALSE,
  ...
)

Value

data.frame

Arguments

xlsxFile

An xlsx file, Workbook object or URL to xlsx file.

sheet

The name or index of the sheet to read data from.

startRow

first row to begin looking for data.

startCol

first column to begin looking for data.

rowNames

If TRUE, first column of data will be used as row names.

colNames

If TRUE, the first row of data will be used as column names.

skipEmptyRows

If TRUE, empty rows are skipped else empty rows after the first row containing data will return a row of NAs.

skipEmptyCols

If TRUE, empty columns are skipped.

rows

A numeric vector specifying which rows in the Excel file to read. If NULL, all rows are read.

cols

A numeric vector specifying which columns in the Excel file to read. If NULL, all columns are read.

detectDates

If TRUE, attempt to recognize dates and perform conversion.

namedRegion

A named region in the Workbook. If not NULL startRow, rows and cols parameters are ignored.

na.strings

A character vector of strings which are to be interpreted as NA. Blank cells will be returned as NA.

na.numbers

A numeric vector of digits which are to be interpreted as NA. Blank cells will be returned as NA.

check.names

logical. If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names

sep.names

(unimplemented) One character which substitutes blanks in column names. By default, "."

fillMergedCells

If TRUE, the value in a merged cell is given to all cells within the merge.

...

additional arguments passed to wb_to_df()

Details

Formulae written using write_formula to a Workbook object will not get picked up by read_xlsx(). This is because only the formula is written and left to be evaluated when the file is opened in Excel. Opening, saving and closing the file with Excel will resolve this.

See Also

wb_get_named_regions() wb_to_df()

Examples

Run this code

xlsxFile <- system.file("extdata", "readTest.xlsx", package = "openxlsx2")
df1 <- read_xlsx(xlsxFile = xlsxFile, sheet = 1, skipEmptyRows = FALSE)
sapply(df1, class)

df2 <- read_xlsx(xlsxFile = xlsxFile, sheet = 3, skipEmptyRows = TRUE)
df2$Date <- convert_date(df2$Date)
sapply(df2, class)
head(df2)

df2 <- read_xlsx(
  xlsxFile = xlsxFile, sheet = 3, skipEmptyRows = TRUE,
  detectDates = TRUE
)
sapply(df2, class)
head(df2)

wb <- wb_load(system.file("extdata", "readTest.xlsx", package = "openxlsx2"))
df3 <- read_xlsx(wb, sheet = 2, skipEmptyRows = FALSE, colNames = TRUE)
df4 <- read_xlsx(xlsxFile, sheet = 2, skipEmptyRows = FALSE, colNames = TRUE)
all.equal(df3, df4)

wb <- wb_load(system.file("extdata", "readTest.xlsx", package = "openxlsx2"))
df3 <- read_xlsx(wb,
  sheet = 2, skipEmptyRows = FALSE,
  cols = c(1, 4), rows = c(1, 3, 4)
)

Run the code above in your browser using DataLab