[R-SIG-Mac] [SOLVED] Package 'xlsx' on Mac OS X using rJava

Simon Urbanek simon.urbanek at r-project.org
Tue Nov 22 22:52:46 CET 2011


James,

On Nov 22, 2011, at 3:54 PM, James Ward wrote:

> First, thanks to Adrian A. Dragulescu, the author of the 'xlsx' package,
> for an immensely useful package!  I hope this Email is useful to someone
> out there, I thought it may be useful to document.
> 
> On Mac OS X 10.6 I had a problem using the package 'xlsx', it could not
> color-shade Excel cells.  One fix is to run the Java process in headless
> mode.  

Yes, that is a known issue in OS X. Note that your example below will only work if no other package has initialized rJava before. A more general solution is to set the system property directly:

.jcall("java/lang/System","S","setProperty","java.awt.headless","true")

Note that this limitation does not exist when running R in JGR (and AWT is usable there).

As for heap size, that can be indeed only set in the java options.

Cheers,
Simon



> In the example below, I show how to run rJava in headless mode, and
> I also increased the Java memory heap size to 1 Gigabyte (mostly to
> demonstrate how to set multiple Java parameters at once.)  Omit or adjust
> the heapsize option as needed.  I include a working example.  (I hope!)
> 
> ################## BEGIN TEST
> ## The option below is used upon initializing the rJava environment
> ## and may be placed in an .Renviron or .Rprofile file
> options( java.parameters = c("-Djava.awt.headless=true", "-Xmx1g") );
> 
> ## Once initialized, load the 'xlsx' package:
> library(xlsx);
> 
> ## The example code follows below:
> wb <- createWorkbook();
> sheet <- createSheet(wb, "Sheet1");
> rows  <- createRow(sheet, rowIndex=1);
> cell.1 <- createCell(rows, colIndex=1)[[1,1]];
> setCellValue(cell.1, "Hello R!");
> ## The following function 'createCellStyle' fails without the fix on a Mac:
> cellStyle1 <- createCellStyle(wb, borderPosition="RIGHT",
>                              borderPen="BORDER_DASHED",
> fillBackgroundColor="yellow",
>                              fillForegroundColor="tomato",
> fillPattern="BIG_SPOTS");
> setCellStyle(cell.1, cellStyle1);
> saveWorkbook(wb, "testExcelWorkbook.xlsx");
> ################## END TEST
> 
> Alternatively, one can manually initialize the Java environment, but you
> must do so before loading the 'xlsx' package, like so:
> 
>     library(rJava);
>     .jinit(parameters=c("-Djava.awt.headless=true", "-Xmx2g") );
>     library(xlsx);
> 
> 
> Now I can try to create Excel files with multiple worksheets, fancy
> cell-shading, and use "freeze panes" on the first row of data!  :-)
> 
> -- 
> James M. Ward
> Computational Biologist and Genome Bioinformaticist
> NIEHS Bioinformatics, RTP, NC
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-SIG-Mac mailing list
> R-SIG-Mac at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-mac
> 
> 



More information about the R-SIG-Mac mailing list