Export both Image and Data from R to an Excel spreadsheet

后端 未结 3 1935
南笙
南笙 2021-01-13 06:15

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()
         


        
3条回答
  •  生来不讨喜
    2021-01-13 07:05

    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)
    

提交回复
热议问题