【问题标题】:Regarding "officer" package and Table function in R关于 R 中的“officer”包和 Table 函数
【发布时间】:2020-05-27 13:54:00
【问题描述】:

我有以下代码: 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") 

我有 2 个问题无法解决,需要帮助:

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

提前谢谢大家!等待您的回复。

【问题讨论】:

  • @shome 感谢您的链接;但它没有提到如何减小要导出到幻灯片的数据框的字体大小。你能帮我提供示例代码吗?
  • @ruser..检查字体相关设置的第一个答案。我正在尝试输入一些代码。

标签: r officer


【解决方案1】:

问题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")

【讨论】:

  • 哇!非常感谢您的解释和示例代码!祝你有美好的一天!
猜你喜欢
  • 1970-01-01
  • 2021-06-22
  • 2019-06-19
  • 2021-10-28
  • 2019-06-25
  • 2012-04-16
  • 1970-01-01
  • 2020-05-27
  • 2016-09-14
相关资源
最近更新 更多