addPlot error

150 views
Skip to first unread message

PS

unread,
Oct 30, 2015, 7:22:17 PM10/30/15
to ReporteRs
I'm using the aheatmap plotting function from the NMF package along with addPlot.  If I plot the heatmaps with no dendrograms, or with only a column dendrogram (across the top of the heatmap), it works well with ReporteRs.  But, if I try to output the heatmap with a row dendrogram (down the side of the heatmap) with addPlot, I get an error. I think aheatmap just uses grid graphics, and I'm pretty sure in the past, I've been able to get this to work, but with the latest version of both packages, I cannot.  

Here's a code example:


library(ReporteRs)
library(NMF)

sessionInfo()
 
# R version 3.2.2 (2015-08-14)
# Platform: x86_64-apple-darwin14.5.0 (64-bit)
# Running under: OS X 10.10.5 (Yosemite)
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# attached base packages:
#   [1] parallel  stats     graphics  grDevices utils     datasets  methods   base     
# other attached packages:
#   [1] ReporteRs_0.8.3     ReporteRsjars_0.0.2 NMF_0.20.6          synchronicity_1.1.4
# [5] bigmemory_4.5.8     bigmemory.sri_0.1.3 Biobase_2.28.0      BiocGenerics_0.14.0
# [9] cluster_2.0.3       rngtools_1.2.4      pkgmaker_0.22       registry_0.3       
# loaded via a namespace (and not attached):
#   [1] Rcpp_0.12.1        magrittr_1.5       MASS_7.3-44        munsell_0.4.2     
# [5] doParallel_1.0.10  colorspace_1.2-6   xtable_1.7-4       gridBase_0.4-7    
# [9] foreach_1.4.3      stringr_1.0.0      plyr_1.8.3         tools_3.2.2       
# [13] grid_3.2.2         gtable_0.1.2       iterators_1.0.8    digest_0.6.8      
# [17] rJava_0.9-8        RColorBrewer_1.1-2 reshape2_1.4.1     ggplot2_1.0.1     
# [21] codetools_0.2-14   stringi_1.0-1      scales_0.3.0       proto_0.3-10  

# Generate random data
n <- 50; p <- 20
datax <- abs(rmatrix(n, p, rnorm, mean=4, sd=1))
datax[1:10, seq(1, 10, 2)] <- datax[1:10, seq(1, 10, 2)] + 3
datax[11:20, seq(2, 10, 2)] <- datax[11:20, seq(2, 10, 2)] + 2
rownames(datax) <- paste("ROW", 1:n)
colnames(datax) <- paste("COL", 1:p)

# Set up power point doc
doc = pptx()
doc = addSlide( doc, "Title and Content" )

# plotting both dendrograms (default) gives an error
doc = addPlot(doc = doc, fun=function(){aheatmap(datax)},  width=9.5, height=6, offx=0.2, offy = 0.1)

#java.lang.ArrayIndexOutOfBoundsException: 2147483647Error in vector.pptx.graphic(doc = doc, fun = fun, pointsize = pointsize,  : 
#  an error occured when executing plot function.

# not plotting the row dendrogram works fine
doc = addPlot(doc = doc, fun=function(){aheatmap(datax, Rowv=FALSE)},  width=9.5, height=6, offx=0.2, offy = 0.1)

writeDoc(doc, "exampleaheatmap.pptx")


I'd appreciate any help. 

 ReporteRs is such a lifesaver for my work!  Thanks so much to the developer for making it.

PS

unread,
Nov 1, 2015, 9:31:50 AM11/1/15
to ReporteRs
 I think aheatmap just uses grid graphics, and I'm pretty sure in the past, I've been able to get this to work, but with the latest version of both packages, I cannot.  

Actually, I just confirmed that with ReporteRs v 0.7.8, I can output aheatmap plots to Power Point, just not with 0.8.2.

David Gohel

unread,
Nov 2, 2015, 4:20:08 AM11/2/15
to ReporteRs
Hi

Thanks,

The issue is identified. It’s a serious one but there should be an available correction soon.


I will let you know as soon as it will be available.


David

David Gohel

unread,
Jan 13, 2016, 6:01:31 AM1/13/16
to ReporteRs
Hi,

There is a new version available on github. It should solve this issue.

David 

Xiaofei Wang

unread,
Sep 19, 2017, 1:21:02 PM9/19/17
to ReporteRs
Dear David,

I got the same error in a different case. Here is the function I used as below. If I used it on subset my real data (first 100 lines), it will work out. But, if on my real data, I will get the error. The real data has 7021090  lines. 


> test<-head(real_data,n=100)
> pairs(test,lower.panel=panel.smooth, upper.panel=panel.cor,diag.panel=panel.hist)
> export2ppt(file="test_100lines.pptx") # Will works well with "test_100lines.pptx" output.

But, error with my real data:

> pairs(real_data,lower.panel=panel.smooth, upper.panel=panel.cor,diag.panel=panel.hist)

> export2ppt(file="real_data.pptx")
Error in .jcall(slide, "I", "add", dml.object, width, height, offx, offy) : 
  java.lang.ArrayIndexOutOfBoundsException


export2office = function(file = "plot", type="PPT", scaling = 90, aspectr=NULL, vector.graphic = TRUE, pointsize=20) {

  file=sub("^(.*)[.].*", "\\1", file)

  if (type=="PPT"|type=="PPTX") {ext=".pptx";type="PPT"} else {ext=".docx";type="DOC"}

  require(ReporteRs)

  captureplot = function() {p = invisible(recordPlot())

                          return(p)}

  p = captureplot()

  plotsize = dev.size()

  plotaspectr = plotsize[[1]]/plotsize[[2]]

  if (!is.null(aspectr)) plotaspectr=aspectr 

  myplot=function(pl=p) print(pl)

  if (type=="PPT") {doc = pptx();doc = addSlide(doc, slide.layout = "Blank");pagesize = dim(doc)$slide.dim} else {doc = docx();pagesize = dim(doc)$page-dim(doc)$margins[c(4,3)]}    

  pageaspectr = pagesize["width"]/pagesize["height"]

  if (pageaspectr>plotaspectr) {xf=plotaspectr/pageaspectr;yf=1} else {xf=1;yf=pageaspectr/plotaspectr}

  w = (scaling/100)*pagesize["width"]*xf;

  h = (scaling/100)*pagesize["height"]*yf

  if (type=="PPT") {doc = addPlot( doc, myplot, vector.graphic = vector.graphic, pointsize = pointsize,

                 offx = (pagesize["width"]-w)/2, offy = (pagesize["height"]-h)/2,

                 width = w, height = h) } else {doc = addPlot( doc, myplot, vector.graphic = vector.graphic, pointsize = pointsize,width = w, height = h)}

  writeDoc( doc, paste0(file,ext) )  

}


export2ppt = function(type="PPT", ...) export2office(type=type,...)




Here is the sessionInfo:

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gdtools_0.1.5       ReporteRs_0.8.8     ReporteRsjars_0.0.2

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.12      knitr_1.17        xml2_1.1.1        bindr_0.1         magrittr_1.5      uuid_0.1-2        munsell_0.4.3     xtable_1.8-2     
 [9] colorspace_1.3-2  R6_2.2.2          rlang_0.1.2       plyr_1.8.4        dplyr_0.7.2       tools_3.3.2       grid_3.3.2        gtable_0.2.0     
[17] png_0.1-7         rvg_0.1.4         R.oo_1.21.0       htmltools_0.3.6   lazyeval_0.2.0    digest_0.6.12     assertthat_0.2.0  tibble_1.3.3     
[25] zip_1.0.0         bindrcpp_0.2      shiny_1.0.4       rJava_0.9-8       purrr_0.2.3       officer_0.1.5     ggplot2_2.2.1     base64enc_0.1-3  
[33] R.utils_2.5.0     mime_0.5          glue_1.1.1        scales_0.4.1      R.methodsS3_1.7.1 httpuv_1.3.5      pkgconfig_2.0.1  

> system("java -version")
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)


Looking forward to hearing from you!

Thanks a lot! I really appreciate it!

David Gohel

unread,
Sep 19, 2017, 1:35:19 PM9/19/17
to ReporteRs
Dear Xiaofei

Can you send a reproducible example?
I don't know how to reproduce real_data. Also , did you try with officer/rvg?

David

Xiaofei Wang

unread,
Sep 20, 2017, 10:24:20 PM9/20/17
to ReporteRs
Hi David,

I tried to explore subset my data to see when I will get the error. If I used first 1 million lines of my data, I won't get the error. But I will get the error when I used 2 million lines. I attached the 2 millions as test_2m.RDdata.

Here is the code and the functions that I used:

options(java.parameters = "-Xmx12000m")

library(ReporteRs)

library(ReporteRsjars)


pairs(test_2m,lower.panel=panel.smooth, upper.panel=panel.cor,diag.panel=panel.hist)

export2ppt(file="test_2m_lines.pptx")


# get error as below.
Error in .jcall(slide, "I", "add", dml.object, width, height, offx, offy) : 
  java.lang.ArrayIndexOutOfBoundsException

# but no error for the first 1 million lines

test_1m<-head(test_2m,n=1000000)

pairs(test_1m,lower.panel=panel.smooth, upper.panel=panel.cor,diag.panel=panel.hist)

export2ppt(file="test_1m_lines.pptx")



# here are the functions


panel.cor <- function(x, y, digits=2, cex.cor)

{

  usr <- par("usr"); on.exit(par(usr))

  par(usr = c(0, 1, 0, 1))

  r <- abs(cor(x, y))

  txt <- format(c(r, 0.123456789), digits=digits)[1]

  test <- cor.test(x,y)

  Signif <- ifelse(round(test$p.value,3)<0.001,"p<0.001",paste("p=",round(test$p.value,3)))  

  text(0.5, 0.25, paste("r=",txt))

  text(.5, .75, Signif)

}


panel.smooth<-function (x, y, col = "blue", bg = NA, pch = 18

                        cex = 0.8, col.smooth = "red", span = 2/3, iter = 3, ...

{

  points(x, y, pch = pch, col = col, bg = bg, cex = cex)

  ok <- is.finite(x) & is.finite(y)

  if (any(ok)) 

    lines(stats::lowess(x[ok], y[ok], f = span, iter = iter), col = col.smooth, ...)# lowess line

    abline(stats::lm(x[ok]~y[ok]),col= "orange") # regression line

     

}


panel.hist <- function(x, ...)

{

  usr <- par("usr"); on.exit(par(usr))

  par(usr = c(usr[1:2], 0, 1.5) )

  h <- hist(x, plot = FALSE)

  breaks <- h$breaks; nB <- length(breaks)

  y <- h$counts; y <- y/max(y)

  rect(breaks[-nB], 0, breaks[-1], y, col="cyan", ...)

}




export2office = function(file = "plot", type="PPT", scaling = 90, aspectr=NULL, vector.graphic = TRUE, pointsize=20) {

  file=sub("^(.*)[.].*", "\\1", file)

  if (type=="PPT"|type=="PPTX") {ext=".pptx";type="PPT"} else {ext=".docx";type="DOC"}

  require(ReporteRs)

  captureplot = function() {p = invisible(recordPlot())

                          return(p)}

  p = captureplot()

  plotsize = dev.size()

  plotaspectr = plotsize[[1]]/plotsize[[2]]

  if (!is.null(aspectr)) plotaspectr=aspectr 

  myplot=function(pl=p) print(pl)

  if (type=="PPT") {doc = pptx();doc = addSlide(doc, slide.layout = "Blank");pagesize = dim(doc)$slide.dim} else {doc = docx();pagesize = dim(doc)$page-dim(doc)$margins[c(4,3)]}    

  pageaspectr = pagesize["width"]/pagesize["height"]

  if (pageaspectr>plotaspectr) {xf=plotaspectr/pageaspectr;yf=1} else {xf=1;yf=pageaspectr/plotaspectr}

  w = (scaling/100)*pagesize["width"]*xf;

  h = (scaling/100)*pagesize["height"]*yf

  if (type=="PPT") {doc = addPlot( doc, myplot, vector.graphic = vector.graphic, pointsize = pointsize,

                 offx = (pagesize["width"]-w)/2, offy = (pagesize["height"]-h)/2,

                 width = w, height = h) } else {doc = addPlot( doc, myplot, vector.graphic = vector.graphic, pointsize = pointsize,width = w, height = h)}

  writeDoc( doc, paste0(file,ext) )  

}


export2ppt = function(type="PPT", ...) export2office(type=type,...)


test_2m.RData

Xiaofei Wang

unread,
Sep 20, 2017, 10:26:17 PM9/20/17
to ReporteRs
I didn't try with officer/rvg because I have not read the manual of them and don't know how to change my code yet.

Thanks a lot!

BEST,

Xiaofei


On Tuesday, September 19, 2017 at 1:35:19 PM UTC-4, David Gohel wrote:

David Gohel

unread,
Sep 28, 2017, 9:50:18 AM9/28/17
to ReporteRs
I was not able to produce the graphic with my 16go mac os... There are two many points in that plot.
I will try later to see how to debug that.

However, even if you would have been able to produce the plot in PPTX, I don't think you would be able to open it. Using a vector graphics format when you have many compounds to visualize is not a good idea - in that case, it is better to use raster graphics.

David

Xiaofei Wang

unread,
Sep 29, 2017, 3:48:19 PM9/29/17
to ReporteRs
Yes, I know it is not a good idea but it is convenient to edit. I did not realized that I won't open it I just see it will be in a large size. If I can't open it, I think I will switch to raster graphics. But, let me know, when you figure it out why there is an error when it run on big dataset. Thanks a lot! Xiaofei
Reply all
Reply to author
Forward
0 new messages