简体   繁体   中英

Unable to specify slide size when exporting a plot from R to a pptx file using R packages OFFICER and RVG

I have created a plot in R, and exported it as a vector object in a powerpoint slide (pptx file), using the "rvg" and "officer" packages (see code below).

I would like to define the dimensions (height and width) of the slides in the pptx create by the code, but I cannot see an option to do this. It is possible to specify the size of the plot object exported, but not the size of the powerpoint slide size.

Is it possible to define the size of the powerpoint slide when exporting a plot from R into a pptx file?

Many thanks for advice/help

#EXAMPLE CODE
#IMPORT LIBRARIES
library(officer)
library(rvg)
#MAKE RANDOM DATA
x<-rnorm(50,1,10)
y<-rnorm(50,2,20)
#WRAP PLOT CODE IN A FUNCTION
plot.code<-function(){
  plot(x,y)
}
#CREATE AND EXPORT PPTX FILE
export.plot<-read_pptx()
export.plot <- add_slide(export.plot, "Title and Content", "Office Theme")
export.plot<-ph_with_vg(export.plot,code=plot.code(),type="body",height=10,width=10)
 #height and width in the above code define the size of the plot, not the slide
print(export.plot, target = "export.plot.pptx")

I have the same question, and haven't yet found a clear answer, but I did figure out a shoddy work-around.

  1. Open Powerpoint and save an empty presentation with slides the size that you want.
  2. In R, read in the powerpoint that you just created.
  3. Add the content in R
  4. Save powerpoint in R.

That will look something like this, with a previously created manually_created.pptx .

doc <- officer::read_pptx("manually_created.pptx") %>%
        # remove the empty slide
        officer::remove_slide(1)

doc %>% 
   # add a new slide to the powerpoint
   officer::add_slide("Title and Content", "Office Theme") %>% 
   # add plot to the slide
   officer::ph_with_vg(code = plot.code(), type = "body", height = 10, width = 10) %>%
   # save the powerpoint
   officer:::print.rpptx("manually_created.pptx")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM