Search code examples
rpngtransparency

How to create and save a 1x1 pixel transparent png using R?


I would like to create a 1x1 pixel transparent png file in R. Is there any way to do so or do i have to "copy" a premade 1x1 pixel png?

I've tryed

empty_frame = imfill(x = 1, y = 1, z = 1, val = "transparent", dim = NULL)

but this does not create a transparent pixel but a 1x1 white pixel.

I need the 1x1 pixel .png file as a placeholder in case there are no data to be plotted.


Solution

  • One way would be using png::writePNG:

    png::writePNG(array(0, dim = c(1,1,4)), "pixel.png")
    

    This creates a transparent 1x1 pixel (with 4 channels set at zero, for each of RGB and alpha), which I confirmed by opening it up in GIMP.