It is simple to print an image and save it on disk just:
fit <- lm(some ~ model)
png(filename=\"your/file/location/name.png\")
plot(fit)
dev.off()
You can do the same thing with the insertImage
function and using the openxlsx
package.
I've copied the example from above, but using the openxlsx package instead:
# Setup
x <- c('tidyverse', 'openxlsx')
sapply(X=x, FUN = require, character.only = TRUE)
# Create graph---------
dcs <- ggplot(mtcars) +
geom_point(aes(cyl, disp))
# Export Pic -----------
pic_path <- "C:/home/dcs.jpeg"
png(filename = pic_path)
plot(dcs)
dev.off()
# Add to a new work book -------------
wb <- openxlsx::createWorkbook()
addWorksheet(wb, "Plots")
insertImage(wb, "Plots", pic_path)
openxlsx::saveWorkbook(wb,
"~/example.xlsx")
# Kill pic
unlink(pic_path)