【问题标题】:Removing slides of a presentation using officer package in R使用 R 中的官员包删除演示文稿的幻灯片
【发布时间】:2018-08-06 19:22:09
【问题描述】:

我正在使用 R 中的官员包将我的图作为输出传输到 power point 演示文稿。

每次我运行代码时,我都想确保 pptx 中没有我想要保存绘图的幻灯片。

现在我知道如何从演示文稿中删除一张幻灯片。但我不知道如何一次删除所有幻灯片。

到目前为止我的代码是:

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

我可以在我的代码中使用officer包中的什么函数来一次删除所有幻灯片?

您的帮助将不胜感激!

问候

【问题讨论】:

    标签: r officer


    【解决方案1】:

    remove_slideindex 参数的文档是

    用法: remove_slide(x, index = NULL)

    index - 幻灯片索引,默认为当前幻灯片位置

    因此,您可以按如下方式一次删除一张幻灯片:

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

    【讨论】:

    • 我尝试包含您的代码,但它没有按我的意愿工作。长度(输出)等于 1,因此只有 1 张幻灯片被删除
    • 文档提到length 返回幻灯片的数量。如果没有,您可能需要在github.com/davidgohel/officer 提出问题
    • 现在我了解了 length() 函数的工作原理。但是您的代码无法一次删除所有幻灯片。它只删除一张幻灯片。我收到此错误:“错误:无效索引 2(1 张幻灯片)”
    • 您的 pptx 中有多少张幻灯片? length 给了你什么?它可能是零索引的,这意味着您需要 n-1
    • 我更新了要按相反顺序删除的代码。似乎每个remove_slide 都会刷新索引
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 2017-10-16
    • 2014-10-19
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多