使用 R 从 powerpoint 中删除图形、图像和数据框
Removing graphs, images, and data frames from a powerpoint using R
我的目的是能够更新我在 R 中生成的 powerpoint,而不必生成新的卡片组。为此,我需要能够以某种方式删除或覆盖我之前制作的 power point deck 中的内容。
据我所知,ph_remove 似乎是唯一执行此操作的代码。遗憾的是,据我所知,它只适用于文本,不适用于图形、数据框或图像。
下面是一些示例代码:
此代码生成包含每种类型对象的 ppt 卡片组:
path_out <- [insert folder directory]
# prep ggplot
p1 <- iris %>%
ggplot() +
geom_point(aes(Sepal.Length,Petal.Length,color = Species), size = 3) +
theme_minimal()
# prep image
img.file <- file.path(R.home("doc"),"html","logo.jpg")
# prep flextable
ft <- flextable(head(mtcars)) %>%
autofit()
# prep editable graph (rvg)
p2 <- dml(ggobj = p1)
my_pres <- read_pptx() %>%
#slide 1
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = "Hello World", location = ph_location_type(type = "title")) %>%
ph_with(value = "A footer", location = ph_location_type(type = "ftr")) %>%
ph_with(value = format(Sys.Date()), location = ph_location_type(type = "dt")) %>%
ph_with(value = "Hello world", location = ph_location_type(type = "body")) %>%
#slide 2
add_slide() %>%
ph_with(value = p1, location = ph_location_type("body")) %>%
#slide 3
add_slide() %>%
ph_with(external_img(img.file, width = 1.39, height = 1.06), location = ph_location_type("body"), use_loc_size = F) %>% # plot an image of specific size
#slide 4
add_slide() %>%
ph_with(ft, location = ph_location(type = "body")) %>%
#slide 5
add_slide() %>%
ph_with(p2, location = ph_location_type("body")) %>% # create a graph that can be edited
print(target = paste0(path_out,"example_v1.pptx"))
此代码尝试从创建的牌组中移除这些对象:
mypres2 <- read_pptx(paste0(path_out,"example_v1.pptx")) %>%
on_slide(index = 1) %>%
ph_remove(type = "title") %>%
ph_remove(type = "ftr") %>%
ph_remove(type = "dt") %>%
ph_remove(type = "body") %>%
on_slide(index = 2) %>%
ph_remove(type = "body") %>%
on_slide(index = 3) %>%
ph_remove(type = "body") %>%
on_slide(index = 4) %>%
ph_remove(type = "body") %>%
on_slide(index = 5) %>%
ph_remove(type = "body") %>%
print(target = paste0(path_out,"example_v2.pptx"))
有谁知道有什么方法可以让我删除它们而无需手动替换它们或重新从头开始生成套牌吗?
ph_remove
正在删除任何类型的现有形状 - 不仅是文本。我已经缩短了您的示例,但确实尝试让它使用不同类型的对象来演示 ph_remove
。通过在 ph_location_type
中使用 new_label
,您可以轻松创建与形状关联的唯一标签并在以后重复使用它们。
library(flextable)
library(rvg)
library(officer)
library(magrittr)
library(ggplot2)
path_out <- "."
# prep ggplot
p1 <- iris %>%
ggplot() +
geom_point(aes(Sepal.Length,Petal.Length,color = Species), size = 3) +
theme_minimal()
# prep image
img.file <- file.path(R.home("doc"),"html","logo.jpg")
# prep flextable
ft <- flextable(head(mtcars)) %>%
autofit()
# prep editable graph (rvg)
p2 <- dml(ggobj = p1)
my_pres <- read_pptx() %>%
#slide 1
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = "Hello World", location = ph_location_type(type = "title", newlabel = "ouaoua")) %>%
ph_with(value = "A footer", location = ph_location_type(type = "ftr", newlabel = "ouaoui")) %>%
ph_with(value = format(Sys.Date()), location = ph_location_type(type = "dt", newlabel = "ouaoue")) %>%
ph_with(value = flextable(head(iris)), location = ph_location_type(type = "body", newlabel = "ouaouo")) %>%
#slide 2
add_slide() %>%
ph_with(value = dml(ggobj = p1), location = ph_location_type("body", newlabel = "ouaoua")) %>%
print(target = file.path(path_out,"example_v1.pptx"))
mypres2 <- read_pptx(file.path(path_out,"example_v1.pptx")) %>%
on_slide(index = 1) %>%
ph_remove(ph_label = "ouaoua") %>%
ph_remove(ph_label = "ouaoui") %>%
ph_remove(ph_label = "ouaoue") %>%
ph_remove(ph_label = "ouaouo") %>%
on_slide(index = 2) %>%
ph_remove(ph_label = "ouaoua") %>%
print(target = file.path(path_out,"example_v2.pptx"))
我的目的是能够更新我在 R 中生成的 powerpoint,而不必生成新的卡片组。为此,我需要能够以某种方式删除或覆盖我之前制作的 power point deck 中的内容。
据我所知,ph_remove 似乎是唯一执行此操作的代码。遗憾的是,据我所知,它只适用于文本,不适用于图形、数据框或图像。
下面是一些示例代码:
此代码生成包含每种类型对象的 ppt 卡片组:
path_out <- [insert folder directory]
# prep ggplot
p1 <- iris %>%
ggplot() +
geom_point(aes(Sepal.Length,Petal.Length,color = Species), size = 3) +
theme_minimal()
# prep image
img.file <- file.path(R.home("doc"),"html","logo.jpg")
# prep flextable
ft <- flextable(head(mtcars)) %>%
autofit()
# prep editable graph (rvg)
p2 <- dml(ggobj = p1)
my_pres <- read_pptx() %>%
#slide 1
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = "Hello World", location = ph_location_type(type = "title")) %>%
ph_with(value = "A footer", location = ph_location_type(type = "ftr")) %>%
ph_with(value = format(Sys.Date()), location = ph_location_type(type = "dt")) %>%
ph_with(value = "Hello world", location = ph_location_type(type = "body")) %>%
#slide 2
add_slide() %>%
ph_with(value = p1, location = ph_location_type("body")) %>%
#slide 3
add_slide() %>%
ph_with(external_img(img.file, width = 1.39, height = 1.06), location = ph_location_type("body"), use_loc_size = F) %>% # plot an image of specific size
#slide 4
add_slide() %>%
ph_with(ft, location = ph_location(type = "body")) %>%
#slide 5
add_slide() %>%
ph_with(p2, location = ph_location_type("body")) %>% # create a graph that can be edited
print(target = paste0(path_out,"example_v1.pptx"))
此代码尝试从创建的牌组中移除这些对象:
mypres2 <- read_pptx(paste0(path_out,"example_v1.pptx")) %>%
on_slide(index = 1) %>%
ph_remove(type = "title") %>%
ph_remove(type = "ftr") %>%
ph_remove(type = "dt") %>%
ph_remove(type = "body") %>%
on_slide(index = 2) %>%
ph_remove(type = "body") %>%
on_slide(index = 3) %>%
ph_remove(type = "body") %>%
on_slide(index = 4) %>%
ph_remove(type = "body") %>%
on_slide(index = 5) %>%
ph_remove(type = "body") %>%
print(target = paste0(path_out,"example_v2.pptx"))
有谁知道有什么方法可以让我删除它们而无需手动替换它们或重新从头开始生成套牌吗?
ph_remove
正在删除任何类型的现有形状 - 不仅是文本。我已经缩短了您的示例,但确实尝试让它使用不同类型的对象来演示 ph_remove
。通过在 ph_location_type
中使用 new_label
,您可以轻松创建与形状关联的唯一标签并在以后重复使用它们。
library(flextable)
library(rvg)
library(officer)
library(magrittr)
library(ggplot2)
path_out <- "."
# prep ggplot
p1 <- iris %>%
ggplot() +
geom_point(aes(Sepal.Length,Petal.Length,color = Species), size = 3) +
theme_minimal()
# prep image
img.file <- file.path(R.home("doc"),"html","logo.jpg")
# prep flextable
ft <- flextable(head(mtcars)) %>%
autofit()
# prep editable graph (rvg)
p2 <- dml(ggobj = p1)
my_pres <- read_pptx() %>%
#slide 1
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = "Hello World", location = ph_location_type(type = "title", newlabel = "ouaoua")) %>%
ph_with(value = "A footer", location = ph_location_type(type = "ftr", newlabel = "ouaoui")) %>%
ph_with(value = format(Sys.Date()), location = ph_location_type(type = "dt", newlabel = "ouaoue")) %>%
ph_with(value = flextable(head(iris)), location = ph_location_type(type = "body", newlabel = "ouaouo")) %>%
#slide 2
add_slide() %>%
ph_with(value = dml(ggobj = p1), location = ph_location_type("body", newlabel = "ouaoua")) %>%
print(target = file.path(path_out,"example_v1.pptx"))
mypres2 <- read_pptx(file.path(path_out,"example_v1.pptx")) %>%
on_slide(index = 1) %>%
ph_remove(ph_label = "ouaoua") %>%
ph_remove(ph_label = "ouaoui") %>%
ph_remove(ph_label = "ouaoue") %>%
ph_remove(ph_label = "ouaouo") %>%
on_slide(index = 2) %>%
ph_remove(ph_label = "ouaoua") %>%
print(target = file.path(path_out,"example_v2.pptx"))