P. Lamarche P. Lamarche - 1 month ago 6
R Question

R tcltk: error when trying to display a png file depending on the OS

This is an issue I am encountering for different pieces of codes I am writing in R.
Basically, I would like to generate a window that displays a picture (a .png file). Following for instance guidances from this or this, I come up with this kind of code:

library(tcltk)
tmpFile <- tempfile(fileext = ".png")
download.file("https://www.r-project.org/logo/Rlogo.png", tmpFile)

tcl("image","create","photo", "imageLogo", file=tmpFile)
win1 <- tktoplevel()
tkpack(ttklabel(win1, image="imageLogo", compound="image"))


This works fine under Mac OS, but not on Linux nor on Windows, where I am displayed such an error message:


[tcl] couldn't recognize data in image file


I can find some workarounds when I want to display graphs, using for instance packages
tkrplot
or
igraph
. Nonetheless, I would be really eager to understand why I got such errors when running my scripts on Linux or Windows, whereas it works just fine on Mac OS.

Apologies in case this issue is obvious, but I haven't found anything about potential differences with the
tcltk
package depending on the OS.

Answer

Tk's native support for PNG was added in 8.6. Prior to that, you need to have the tkimg extension loaded into Tk to add the image format handler required. If your installation of Tcl/Tk that R is using is set up right, you can probably make it work with:

tclRequire("Img")

once you've initialised things sufficiently. Yes, the name used internally is “Img” for historical reasons, but that's just impossible to search for! (This is the key thing in this mailing list message from way back.)

However, upgrading the versions of Tcl and Tk to 8.6 is likely to be a better move.

Comments