Portably Append R Data to Excel Sheets (Without Overwriting)

276 Views Asked by At

Background

I'm writing a .R script on my computer (Windows), which (1) must maintain a Log.xlsx file recording data from its runs; and (2) will be run by a colleague on a separate VM (also Windows).

Requirements

I'm looking for a package in R that will (1) let me append R data (from a data.frame) immediately after the last nonempty row, in an existing sheet within that existing Log.xlsx workbook; while (2) remaining easily portable, such that any necessary packages can be automatically installed by the script itself (via pacman::p_load()*) if the VM does not already have them installed.

* I ensure the presence of pacman using base functionality:

if(!"pacman" %in% installed.packages()) {
    install.packages("pacman")
}

Preferences

Since this log will grow quickly — the script runs daily and will log many dozens of records per run — I would prefer to avoid the approach proposed here, which simply loads all the existing worksheet data into R, where it combines the datasets and then (over)writes the result as a new .xlsx (or .csv) file. I would likewise prefer to avoid the (better?) approach proposed here, which similarly loads and combines everything in R, but then overwrites the sheet within the workbook, rather than overwriting the workbook itself.

In short, I strongly prefer "granular appendability", to simply add new data to the end of an existing sheet, without having to load, combine, and (over)write the entire sheet's (let alone workbook's) worth of data in R.

Roadblock

I have investigated the packages xlsx, openxlsx (which seems to have some stability issues), and XLConnect. While XLConnect seems particularly relevant (see XLConnect::appendWorksheet()) and portable even across operating systems, there remains the problem of Java. At the moment, pacman::p_load() will successfully install XLConnect and xlsx (among others), yet any attempt to load these two packages (via library()) results predictably in

Error: package or namespace load failed for ‘xlsx’:
 .onLoad failed in loadNamespace() for 'rJava', details:
  call: fun(libname, pkgname)
  error: JAVA_HOME cannot be determined from the Registry

I could obviously correct this issue on my computer, by following the instructions here to install the version of Java (32- or 64-bit) compatible with my version of R. However, I would like to keep the script self-contained, such that it handles its own dependencies and runs painlessly on the VM, regardless of which "bit-versions" of R and Java are installed on the VM. In particular, I don't want to force my colleague to manually install the appropriate version of Java.

In Conclusion

While the second solution (which apparently avoids the Java roadblock) will do in a true pinch, any help to achieve my listed Preferences would be greatly appreciated!

0

There are 0 best solutions below