Alteryx Designer

Find answers, ask questions, and share expertise about Alteryx Designer.
SOLVED

Reading png images into R

Highlighted
7 - Meteor

I'm trying to use some images within an R plot.  I would like to feed the images directly into the R tool and leverage in a plot.  The only way I've been able to get it to work is save all the images to a folder and read back into R using the readPNG function (png package).  I've tried converting the images to binary but that did not work either.  Is there another R package available that will allow me to read the Alteryx image format without having to save them to disk?  Any examples of how to do this?

 

Thanks!

Highlighted
Alteryx
Alteryx

Hi Landry,

 

The R tool can only read the data in via the connection as a list or as a dataframe. This limits your options to storing the PNG file as/in a dataframe and then converting it back using R code. Depending on what you are going to do with it as to whether this is actually a good idea. If you are planning to search the image looking for patterns then it may be practical to convert the image to binary, but in most cases saving it to disk or blob in a DB and then reading it back in using R script is going to be best (and by far and away easiest).

 

Kane

Highlighted
7 - Meteor

Thanks, Kane!

 

My intent was to show profile pics within a hierarchy using igraph, so it can be up to 100 png images or more.  I'll look into loading them as dataframe and converting, but if that doesn't work out I'll keep the approach I have to save to disk and read back in.  Thanks!

Highlighted
ACE Emeritus
ACE Emeritus

Hello - just a couple thoughts from outside the box, and therefore possibly inapplicable:

 

Are the images actually generated as part of the workflow, or loaded by the workflow and then passed to R?  If not generated during the workflow, consider not loading them at all: just pass the image path to R; (or a vector of paths) and let R do the only image load.

 

Another thought is, depending on how the final output is rendered, could the path be passed all the way through? (e.g. if rendered as HTML, all it's going to do is store the object somehow and then reference it... perhaps if you could make the HTML put an "img" tag in the relevant cell that references the original image: you can have no image loading at all, ever, until someone actually loads the resulting HTML and views it. This would also allow you to change the image at any time, as long as you use the same filename, without worrying if the existing rendering will pick it up or not).

 

Anyway - just a couple thoughts.

Highlighted
7 - Meteor

Thanks for the ideas.  The images are pulled into the workflow at run time, so it grabs the latest photo.  I wish I could easily deploy it as html but for now the hierarchy plots will be rendered as images or PDFs.

 

Hopefully one day we'll have Alteryx Server so we can publish to the gallery and take the html approach you mentioned.

 

Thanks

Highlighted
5 - Atom

How do you convert the dataframe back to an image?

Highlighted
5 - Atom

Can I pass the image into the R tool directly and manipulate it?

 

 

What I am trying to do is create a graph in the alteryx R tool and paste that onto an image that is stored in my folder.

Highlighted
Alteryx
Alteryx

Hi @rsillers

 

You would most likely be best either

  • Reading the image via r script in the R Tool and using something like Magick or EBImage to combine them.
  • Outputting your graph back to Alteryx and using the Overlay tool to combine the images.

 

 

Highlighted
5 - Atom

Can I read the image directly in the Rscript?

 

Reading directly in the R tool --

This the code I have in my rscript that works when I run it in R:

bg1 <- image_read("SLIDE_1.png")

 

But I get this error:

R (20) Error in magick_image_readpath(enc2native(path), density, depth, strip) :

 

OR

 

If I use an image input tool and read it in as a dataframe and convert it to a png --

code in the alteryx R tool to read in image:

bg1 <- read.Alteryx("#2", mode="data.frame")

 

I've tried using this to convert it to a png:

library(gridExtra)
png(filename = "bg1.png", width=480,height=480,bg = "white")
grid.table(bg1)
dev.off()

 

and have also tried this:

bg1 <- image_convert(bg1, "png")

 

and then the code to combine the plot is this:

out1 <- image_composite(image_scale(bg1, "x1500"), fig1, offset = "+110+400" )

 

I get this error either way:

R (20) Error: The 'image' argument is not a magick image object.

Labels