On Thu, 8 Mar 2018, Artur T. wrote:
I want to call an R function for drawing stacked histograms using
gretl's foreign block. As only numeric values (scalar, matrix) can
be passed to R, I first write a temporary txt-file containing the
string for the path+file-name for the plot into $dotdir.
Ah, we've assumed up till now that anyone calling an R function
within gretl will just be interested in numerical stuff. However,
it's not difficult to pass gretl strings to R (he says, trusting
that R, as gretl, does everything in UTF-8). So that's now enabled
in git.
Then I call the R function and read-in the txt-file.
Using R, this function works fine. But it doesn't work properly when
calling from inside gretl. I obtain the error:
<error>
External command failed
*** error in function Rhist, line 8
</error>
Not sure what precisely was going wrong there, but anyway, it's no
longer necessary to create an intermediate string-passing function.
I'll append a working example below, but I should state one thing
first (I'll post a more comprehensive note on this to gretl-users).
With current gretl, please DO NOT write any material to "dotdir"
that you wish to survive after the current gretl session has ended.
dotdir is intended for temporary files, and we now clean it up on
exit. If you want to specify a directory for writing files that
should persist, use $workdir rather than $dotdir.
That said, here's a revision of your script that works with current
git:
<hansl>
set verbose off
set R_functions on
foreign language=R
HIST_R <- function(X, fname) {
library(Cairo)
library(cairoDevice)
H1 <- hist(X[,1])
H2 <- hist(X[,2])
H3 <- hist(X[,3])
CairoPNG(filename=fname, width=600, height=600, pointsize=26, bg=NA, res=NA)
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), xlab=NULL, ylab=NULL, main=NULL)
plot(H3, add=T, col=rgb(1,1,1,1/2), xlab=NULL, ylab=NULL, main=NULL)
dev.off()
ret <- 1
return(ret)
}
end foreign
# example call
matrix M = mnormal(200, 3)
fname = sprintf("%s/Rhistcheck.png", $workdir)
rret = R.HIST_R(M, fname)
</hansl>
The above writes Rhistcheck.png OK but there's one oddity: there's a
momentary flash of what I presume is an R plot window. Do you know
how to prevent that?
Allin