looking for a way to use foundry code repository to write a visualisation created via an R-Script using ggplot2 into the foundry file system.
looking for a way to use foundry code repository to write a visualisation created via an R-Script using ggplot2 into the foundry file system.
Great question! This is answered in the documentation here: https://www.palantir.com/docs/foundry/code-workbook/transforms-unstructured/#example-saving-a-plot-to-a-pdf It is possible to write to an output FileSystem. This can be useful to write non-tabular data formats including images, PDFs, text files, and so on.
Call new.output() to instantiate a TransformOutput. Learn more about the FileSystem API.
You can only write files using TransformOutput in nodes that are saved as datasets. You cannot write files using TransformOutput in the console.
Here is an example with a scatterplot (saved to a PDF to maintain good resolution):
plot_pdf <- function() {
library(ggplot2)
theme_set(theme_bw()) # pre-set the bw theme
data("midwest", package = "ggplot2")
# Scatterplot
gg <- ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method="loess", se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(subtitle="Area Vs Population",
y="Population",
x="Area",
title="Scatterplot",
caption = "Source: midwest")
output <- new.output()
output_fs <- output$fileSystem()
pdf(output_fs$get_path("my pdf example.pdf", 'w'))
plot(gg)
}