Integrating Excel Front end with R backend
By: Surobhi Deb
Tags: Rstudio, Efficiency, Easy, Excel, Analytics

Are you someone who loves the simplicity and transparency that a humble Excel sheet offers but is bogged down by the same when it comes to analyzing the massive amount of data which can sometimes lead you to wait forever staring at a stuck sheet and wishing it doesn’t hang? If this sounds familiar then you should definitely try integrating the frontend of Excel with the backend of R Engine if you haven’t done it yet. I will explain using a short illustration on how this is done.

In my everyday work, I am needed to analyze huge data sets, slicing and dicing them at different levels, pivoting, using formulae across several thousand rows, and so on. As a result, I often used to experience hung Excel sheets, and eventually, that became a big roadblock in my daily work life. So I was thinking of a way this can be handled better and came across a package called “openxlsx” in R. I started using this package to populate Excel files from R and learned a little bit more about it every other day through practice. This turned out to be a hugely successful effort and I can proudly say that I am free of seeing hung Excel windows for life.

I would like to add that “openxlsx” is not the only package that achieves this. There are others like “xlsx” and “XLConnect” in R that outputs Excel files but we will limit the scope of this article to discussing the former one.

The description for the said package as per the official R documentation: “This R package simplifies the creation of .xlsx files by providing a high-level interface to writing, styling, and editing worksheets. Through the use of Rcpp , read/write times are comparable to the xlsx and XLConnect packages with the added benefit of removing the dependency on Java.”

Let’s look at few functionalities with an example.

# Installing the required packages

install.packages("openxlsx")

install.packages("data.table") # Requiring the packages to be used in the code

library(openxlsx)

library(data.table) # Reading the input files

file1<-fread("file_1.csv")

file2<-fread("file_2.csv")

file3<-fread("file_3.csv")

In the above example, I am reading three input files just for the purpose of demonstration. In reality, you might read one file and create different tables out of it based on your requirement.

## Creating the Workbook and Adding required number of sheets

wb <- createWorkbook()

addWorksheet(wb, "First_worksheet")

addWorksheet(wb, "Second_Worksheet") ## Adding content to the worksheets

writeData(wb,"First_worksheet",file1)

writeData(wb,"Second_Worksheet",file2) ## Defining the Styles

negStyle <- createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")

The format that you create using the createStyle() option will be used in the cells that you wanted to highlight in the next conditional formatting step.

conditionalFormatting(wb, "First_worksheet",cols = 1:ncol(file1), rows = 2:nrow(file1),rule = ">50", style = negStyle)

This is a particularly interesting step. We pick the range on which the conditional formatting will apply using the rows() and cols() attribute, then we specify the formula using the rule() attribute and finally the style. This applies the custom style to the qualified cell in the selected worksheet.

## Adding Filters

addFilter(wb, 1, row = 1, cols = 1:ncol(file1))

addFilter(wb, 2, row = 1, cols = 1:ncol(file2))

This command will add filters to the data tables on both sheets. There is a myriad of other formatting/editing functions that come in handy based on the specific problem that one is working on. You can find the official document here.

## Printing the final output

saveWorkbook(wb, file = "My_file.xlsx"), overwrite = TRUE)

Finally, you can print the output using the above command.

The final output will look something like this.

Hope this article has been useful and can help get you kick-started in your journey towards having error-free Excel sheets for life.