Dear all,
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. 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>
and in the CLI error
<CLI>
gretl_R_function_exec: R_catch failed on HIST_R
</CLI>
Here is the script:
<hansl>
set verbose off
set R_functions on
function void Rhist (matrix X, string fname)
# MAIN FUNCTION
string sdir = sprintf("%s/tmpRhist.txt", $dotdir)
outfile @sdir --write --quiet # write the path+file name into a
txt-file
printf "%s\n", fname
outfile --close
ret = R.HIST_R(X) # Should "R.HIST_R(X)" also just work?
end function
foreign language=R
HIST_R <- function(X) {
library(Cairo)
library(cairoDevice)
# FIXME
# for (i in 1:dim(X)[2]){
# #v <- as.numeric()
# H$i <- numeric( hist(X[,i]) ) #,breaks=seq(mn,mx,bin))
# }
H1 <- hist(X[,1])
H2 <- hist(X[,2])
H3 <- hist(X[,3])
# read-in "path+file name" string
fname <- paste(readLines("/home/at/.gretl/tmpRhist.txt"),
collapse=" ")
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
#---------
matrix M = mnormal(200,3)
string fname = sprintf("%s/Rhistcheck.png", $dotdir)
Rhist(M, fname)
</hansl>
Best,
Artur