On Fri, 9 Mar 2018, Artur Tarassow wrote:
Thank you for this updated script, Allin. It works fine. However,
here
(ubuntu 17.10.) the figure appears and actually stays permanently. I
can't even close it without closing gretl. For the moment this doesn't
bother me. And no, I did not find an option to avoid the figure showing
up... :-/
This is maybe off-topic, but I took a further look. I know that R is
capable of producing very nice data graphics, but the language to
control graphics seems to me a rabbit-hole of doom. Comments beow
relate to R-3.4.3 on Fedora 26.
The two packages you loaded in your script at
http://lists.wfu.edu/pipermail/gretl-users/2018-March/013035.html ,
namely Cairo and cairoDevice, are not complementary, they're
competitors (not unusual for R packages). So I've tried each of them
individually (script below). Both of them work when the script is
run in gretlcli -- and in that context cairoDevice is better in one
way: it respects your request to use the Times font, which is
ignored by the Cairo package. However, when run via the gretl GUI
the cairoDevice variant fails. Neither of the packages respects
your par() setting of "las=1" (axis labels always horizontal).
The graphics system seems to depend on a bunch of inscrutable global
variables over which the packages have an insecure degree of
control. Maybe it's unfair, but I have a sense of package writers
not quite knowing what they're doing in this area. Or maybe the API
has been changed since the packages were written.
As for the flashing (or persistent) X11 window showing a simplified
version of the plot we're trying to write to PNG, it seems to be a
feature of R that you can't turn that off. However, by sticking
"dev.off()" before the call to Cairo() I was able (on my system at
any rate) to get the X11 window just to flash, and not persist, when
using gretl_x11. (Using gretlcli it always just flashed.)
<hansl>
set verbose off
set R_functions on
foreign language=R
HIST_R1 <- function(X, fname) {
# package Cairo 1.5-9 (2015-09-26)
library(Cairo)
H1 <- hist(X[,1])
H2 <- hist(X[,2])
H3 <- hist(X[,3])
dev.off()
# dimensions in pixels
CairoPNG(filename=fname, width=600, height=600,
pointsize=14)
par(family="Times", las=1)
plot(H1, col=rgb(0,0,0,1/2), xlab="", main="")
plot(H2, add=T, col=rgb(0.5,0.5,0.5,1/2))
plot(H3, add=T, col=rgb(1,1,1,1/2))
dev.off()
0
}
HIST_R2 <- function(X, fname) {
# package cairoDevice 2.24 (2017-01-06)
library(cairoDevice)
H1 <- hist(X[,1])
H2 <- hist(X[,2])
H3 <- hist(X[,3])
dev.off()
# dimensions nominally in inches
Cairo_png(filename=fname, width=7, height=7,
pointsize=14)
par(family="Times", las=1)
plot(H1, col=rgb(0,0,0,1/2), xlab="", main="")
plot(H2, add=T, col=rgb(0.5,0.5,0.5,1/2))
plot(H3, add=T, col=rgb(1,1,1,1/2))
dev.off()
0
}
end foreign
# example calls
matrix M = mnormal(200, 3)
# does not respect choice of font family via par()
printf "Calling R.HIST_R1\n"
fname = sprintf("%s/Rhist1.png", $workdir)
scalar rret = R.HIST_R1(M, fname)
print rret
# fails when called via gretl GUI, but OK in gretlcli
printf "Calling R.HIST_R2\n"
fname = sprintf("%s/Rhist2.png", $workdir)
scalar rret = R.HIST_R2(M, fname)
print rret
</hansl>
Allin