如果我们想得到'url_list'的值和索引,而不是一个嵌套循环,只需在'url_list'序列上循环,该序列返回索引并使用该索引来子集相应的'url'。此外,更改
colNames = FALSE
在里面
read.xlsx
默认情况下为TRUE,因此第一行将作为列名
for (i in seq_along(url_list)) {
# create the 'url_path' so that we don't have to change the OP's code below
url_path <- url_list[i]
df <- openxlsx::read.xlsx(url_path, rows = c(5:7),
cols = c(5:7), na.strings = "-", colNames = FALSE)
temp <- read_pptx()
report <- add_slide(temp, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value='content', location = ph_location_label(ph_label = "Content Placeholder 2"))
report <- add_slide(report, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = df, location = ph_location_label(ph_label = "Content Placeholder 2"))
n_slides <- length(report)
for (i_slide in 2:n_slides) {
report <- report %>%
ph_with(value = format(Sys.Date()), location = loc_dt) %>%
ph_with(value = i_slide, location = loc_slidenum)
}
print(report, target=glue('report{i}.pptx'))
}
或使用
iwalk
library(purrr)
iwalk(url_list, ~ {
url_path <- .x
i <- .y
df <- openxlsx::read.xlsx(url_path, rows = c(5:7),
cols = c(5:7), na.strings = "-", colNames = FALSE)
temp <- read_pptx()
report <- add_slide(temp, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value='content', location = ph_location_label(ph_label = "Content Placeholder 2"))
report <- add_slide(report, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = df, location = ph_location_label(ph_label = "Content Placeholder 2"))
n_slides <- length(report)
for (i_slide in 2:n_slides) {
report <- report %>%
ph_with(value = format(Sys.Date()), location = loc_dt) %>%
ph_with(value = i_slide, location = loc_slidenum)
}
print(report, target=glue('report{i}.pptx'))
})