我有以下代码,其中我: 1)使用officer将数据帧及其摘要填充到PPT中2)使用表函数从数据帧计算摘要
library(officer)
df1 <- data.frame(
Country = c("France", "England", "India", "America", "England", "India"),
City = c("Paris", "London", "Mumbai", "Los Angeles", "Surrey", "Chennai"),
Order_No = c("1", "2", "3", "4", "5", "6"),
State = c("Yes", "No", "Yes", "No", "Yes", "Transit"),
stringsAsFactors = FALSE
)
my_pres <- read_pptx()
my_pres <- add_slide(my_pres, layout = "Title and Content", master = "Office Theme")
my_pres <- ph_with(my_pres, value = "heading", location = ph_location_type(type = "title"))
my_pres <- ph_with(my_pres, value = df1, location = ph_location_type(type = "body"), header = TRUE)
print(my_pres, target = "first_example.pptx")
table1 <- table(df1$Country, df1$State)
overview.df <- data.frame(Country = rownames(table1),
Not.Delivered =table1[,1],
In.Transit = table1[,2],
Delivered = table1[,3])
my_pres <- add_slide(my_pres, layout = "Title and Content", master = "Office Theme")
my_pres <- ph_with(my_pres, value = "Summary", location = ph_location_type(type = "title"))
my_pres <- ph_with(my_pres, value = overview.df, location = ph_location_type(type = "body"), header = TRUE)
print(my_pres, target = "first_example.pptx") 我这里有两个问题,我无法解决,需要帮助:
1)我无法在PPT中更改输出的字体和字号。这里有人能帮我一下吗2)当我从"table1“计算数据帧"overview.df”时,有时输出包含“是”、“否”和“运输”,上面的代码对它有效。但有时,输入数据框只包含“是”和“否”,我的代码失败。因此,我想要一个泛型代码来替换以下行:
table1 <- table(df1$Country, df1$State)
overview.df <- data.frame(Country = rownames(table1),
Not.Delivered =table1[,1],
In.Transit = table1[,2],
Delivered = table1[,3])提前感谢大家!等待您的回复。
发布于 2020-02-15 17:59:09
问题1:您需要使用fpar和/或block_list从R控制字体的大小、颜色等。默认情况下,使用的字体是模板中定义的字体。在您的示例中,您没有提供任何模板,因此使用默认模板。
问题2:也许可以使用flextable::proc_freq
library(officer)
library(flextable)
df1 <- data.frame(
Country = c("France", "England", "India", "America", "England", "India"),
City = c("Paris", "London", "Mumbai", "Los Angeles", "Surrey", "Chennai"),
Order_No = c("1", "2", "3", "4", "5", "6"),
State = c("Yes", "No", "Yes", "No", "Yes", "Transit"),
stringsAsFactors = FALSE
)
# this is a "proc freq" like ----
pf <- proc_freq(df1, "Country", "State")
pf <- fontsize(pf, size = 11, part = "all")
pf <- padding(pf, padding = 1, part = "all")
pf <- valign(pf, valign = "top", part = "all")
pf <- autofit(pf)
my_pres <- read_pptx()
my_pres <- add_slide(my_pres, layout = "Title and Content", master = "Office Theme")
my_pres <- ph_with(my_pres, value = "heading", location = ph_location_type(type = "title"))
my_pres <- ph_with(my_pres, value = df1, location = ph_location_type(type = "body"), header = TRUE)
my_pres <- add_slide(my_pres, layout = "Title and Content", master = "Office Theme")
my_pres <- ph_with(my_pres, value = "Summary", location = ph_location_type(type = "title"))
my_pres <- ph_with(my_pres, value = pf,
location = ph_location_type(type = "body"), header = TRUE)
print(my_pres, target = "first_example.pptx")

https://stackoverflow.com/questions/60181026
复制相似问题