Wednesday, September 25, 2019

R - Importing an Excel file

The R-library openxlsx offers a performant tool for importing Excel files.

Example for openxlsx::read.xlsx:

library(openxlsx)
openxlsx::read.xlsx(file, 
  sheet = 1, 
  colNames = T, 
  rows = seq(2,100)

This package performs better than the read.xlsx in package xlsx. However, there is one thing to notice: columns in date format will not be correctly handled by openxlsx::read.xlsx. The function xlsx::read.xlsx handles them correctly.

Example for xlsx::read.xlsx:

library(xlsx)
xlsx::read.xlsx(file,
  sheetIndex = 1,
  header = T, 

  rowIndex = seq(2,100)

There is also read.xlsx2 function in the library xlsx, which is written in java, and performs better than xlsx::read.xlsx.

library(xlsx)
xlsx::read.xlsx2(file,
  sheetIndex = 1,
  header = T, 

  rowIndex = seq(2,100)

Using the xslx functions requires that the environments is aware of the location of the java runtime:

Sys.setenv(JAVA_HOME='C:\\ieu\\java\\openjdk-11')