Quickly export multiple R objects to an Excel Workbook

The function,  save.xlsx(),  is very useful to compile different data into one excel spreadsheet.  My problem in running this function was that I could not install “xlsx” package on my computer:


> library("xlsx", quietly = TRUE)
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: inDL(x, as.logical(local), as.logical(now), ...)
error: unable to load shared object 'C:/Users/jonathan.kim/Documents/R/win-library/3.1/rJava/libs/x64/rJava.dll':
LoadLibrary failure: The specified module could not be found.

In addition: Warning messages:
1: package ‘xlsx’ was built under R version 3.1.1
2: package ‘rJava’ was built under R version 3.1.1
Error: package ‘rJava’ could not be loaded

If you get this error, run the following code to load “xlsx” pacakge:

if (Sys.getenv("JAVA_HOME")!="")
Sys.setenv(JAVA_HOME="")
library("xlsx", quietly = TRUE)

statMethods blog

Working with a business audience, I am frequently called upon to send analytic results to clients in the form of Excel Workbooks. The xlsx package facilitates exporting tables and datasets Excel, but I wanted a very simple function that would let me easily export an arbitrary number of R objects to an Excel Workbook in a single call. Each object should appear on in own worksheet, and the worksheets should be named after their objects.

Specifically, the function

should save the R objects mtcars (a data frame),  Titanic (a table),  AirPassengers (a time series) and state.x77 (a matrix) to the workbook myworkbook.xlsx. Each object should be in it’s own worksheet and the worksheet should take on the name of the object.

One solution was to write a wrapper for write.xlsx() function in the xlsx package.

Then

spreadsheet

The method will work for data frames, matrices, time series, and tables, and has simplified my workflow greatly.

View original post

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s