Search code examples
rofficer

Removing slides of a presentation using officer package in R


I am working with officer package in R to transfer my plots as outputs to a power point presentation.

Every time I run the code, I want to make sure that there are no slides in the pptx in which I want to save my plots.

Right now I know how to remove a single slide from the presentation. But I have no idea of how to remove all the slides at once.

My code so far is:

    library(officer)

    # reading template slides and adding slide in which I want my plot 

    doc <- read_pptx("U://30-Power & Water//25 Renewables//WORK//Config//Templates//Template.pptx")

    doc <- add_slide(doc, layout = "Title and Content",master = "Office Theme")
    doc <- ph_with_gg(doc, value = Sweihan ) #plot = ggplot

    # Reading the output slides and removing slides (it is just removing one slide so far)    

    output <- read_pptx("U://30-Power & Water//25 Renewables//WORK//Result//Result.pptx")

    output <-  remove_slide(output)

    print(output, target = "U://30-Power & Water//25 Renewables//WORK//Result//Result.pptx" ) %>% invisible()

    # tranfering results to output ppt file
    print(doc, target = "U://30-Power & Water//25 Renewables//WORK//Result//Result.pptx" ) %>% invisible()

what function can I use in my code using officer package to remove all the slides at once?

Your help will be appreciated!

Regards


Solution

  • The documentation for index argument in remove_slide is

    Usage: remove_slide(x, index = NULL)

    index - slide index, default to current slide position

    Hence, you can remove slide one at a time as follows:

    for (n in rev(seq_len(length(output)))) {
        remove_slide(output, n)
    }
    
    #show number of slides
    length(output)