library(ggplot2)
library(grid)
# Create a simple histogram plot
simpleHist = data.frame(x=rnorm(1e2)) |>
ggplot(aes(x=x)) +
geom_histogram()
# Open a new PDF device, but
#discard the output instead of saving it to a file
pdf(NULL)
# Plot the ggplot object on the current device
simpleHist
# Force the plot to be drawn on the device,
#even though the output is being discarded
grid.force()
# Export the plot to an XML file
AddXML(simpleHist, file = "histogram.xml")
# Close the current PDF graphic device
dev.off()
#Cleaning up afterwards
unlink("histogram.xml")
Run the code above in your browser using DataLab