tomwenseleers / export

R package for streamlined export of graphs and data tables.
192 stars 35 forks source link

graph2office failure #34

Closed CdeMills closed 3 years ago

CdeMills commented 3 years ago

Hello, I have a R script where I export a graph, first as to png, then as pptx as: Cairo(file="Cairo_Rg_96dpi.png", type="png", bg="white", units="pt", width=600, height=260, pointsize=12*96/72, dpi=96) hplot = plot(oled$hour, oled$Rg, col=oled$OLED, pch=16, xlab='hours', ylab='Rg', main='Rg evolution over time') graph2office(x=hplot, file='Cairo_Rg', vector.graphic = TRUE)

Which fails with Error in read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, : StartTag: invalid element name [68]

I tried on three machines: 2 are Windows 10, 1 in CentOS-8. R info R version 4.0.2 (2020-06-22) Platform: x86_64-redhat-linux-gnu (64-bit) Running under: CentOS Linux 8 (Core) Rtools 4.0, evertything similar on three platforms "export" package installed from github

I have this error on the CentOS-8 and one of the Windows 10 machine. Yesterday I installed R + Rstudio + Rtools on another Win10 machine, R was never installed before on it; and it worked, i.e. the plot was exported as .pptx file.

Any hint ?

TIA

Pascal Dupuis

CdeMills commented 3 years ago

Hello, sorry for the noise the last lines should read:

hplot = plot(oled$hour, oled$Rg, col=oled$OLED, pch=16, xlab='hours', ylab='Rg', main='Rg evolution over time') legend('bottomleft', legend= oled$Comment[!duplicated(oled$OLED)], col=oled$OLED[!duplicated(oled$OLED)],pch=16,title="OLED number", text.font=4, bg='lightblue') graph2office(x=hplot, file='Cairo_Rg', vector.graphic = TRUE) dev.off()

I expected dev.off() to flush the graph to the png file, it seems it does but also set hplot to NULL

Maybe you could try detecting this issue and display a more meaningful message ?

Regards

Pascal

cvanderaa commented 3 years ago

Thank you for using our package! Happy to see this worked out for you in the end. However, I would like to make some comments about the way you use graph2office. You can use it in three ways:

  1. Supply the plot as an object x = plotObject. Currently, we only support ggplot and lattice graphs to be passed as objects
  2. Supply the plot as a function x = function() plot(...)
  3. Do not supply the plot with x. The graph is extracted from the currently opened device.

See the Examples section in ?graph2office for a reproducible example of the three usages.

What you do is not what you think. As you said hplot is NULL, but it is true from the beginning because plot only generates the graph and does not return any value. When using plot, I would suggest using methods 2. or 3.. Actually, you are using method 3. in your example. Indeed, the graph is extracted from your opened Cairo device.

Btw, is there a reason you are using Cairo? We have implemented graph2bitmap (for PNG, TIFF, JPEG) that does a similar thing, and that manages the devices automatically so you don't have to mess with dev.off. Again, the same usage applies as for graph2office.

I hope this helps ;-)

CdeMills commented 3 years ago

Hello, thanks for your kind reply. I'm a very long time user of Octave / MatLAB, and some of the intricacies of R are a mystery to me. Actually, I uses the 'interactions' package, which add some layers above ggplot. So case 1 is not possible, case 2 is ... well, difficult to me as I never wrote R functions. So I guess I fall back on case 3 without knowing. In MatLAB, hplot = plot() stores a "graphics handle" which permits to redraw the plot and changes properties.

About Cairo ... there's no good reason. I found the idea on a tutorial page, and that's all. Actually I have no idea about the pros and cons of using Cairo vs pdf / png / ...

Regards

Pascal