Multiple plots / graphs from a single R script

Is there a way to handle this in KNIME? I have an R script that was written by a collaborator that would produce several different plots for them when run through RStudio. However when I run it as an R snippet in KNIME I only see one of the plots. If I go in to the snippet and select “Eval Script”, eventually in workspace (on the right side) I see several items that should be plots. However they all seem to be displaying the same plot when I select “Show Plot”.

It seems that the obvious answer is probably to just split up the R code into multiple snippets so that each produces only the plots I need, but I was wondering if there is a way for one R snippet to produce multiple plots and have KNIME handle them correctly.

thank you!

You could determine a local path and then tell R to save various graphics in a row. I attached an example workflow that does that. You might use that as a starter.

The R View shows one plot but saves more than one. I tried that with just a R scrip node but it gave me an error message.


var_name_pdf_file <- paste0([[“var_path_graphic”]], “rplot.pdf”)
var_name_png_file <- paste0([[“var_path_graphic”]], “rplot.png”)


p1 <- ggplot(rframe, aes(x=x, y=y, color=z)) + geom_point()


p2 <- ggplot(rframe, aes(x=y, fill=z)) + geom_density(alpha=0.5)

ggsave(p1, filename=var_name_pdf_file)
ggsave(p2, filename=var_name_png_file)

kn_example_r_multiple_graphic_export.knar (241.5 KB)

You should split the R script. One part (preparation of all plots) will go into a R source or R to R node, the other parts (rendering each plot) into dedicated R view nodes, one for each plot.

One can also streamline such an approach using a loop and a list of plot names for the latter part in conjunction with image to cell.

1 Like