首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于R中的"officer“包和表函数

关于R中的"officer“包和表函数
EN

Stack Overflow用户
提问于 2020-02-12 12:14:08
回答 1查看 496关注 0票数 1

我有以下代码,其中我: 1)使用officer将数据帧及其摘要填充到PPT中2)使用表函数从数据帧计算摘要

代码语言:javascript
复制
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”时,有时输出包含“是”、“否”和“运输”,上面的代码对它有效。但有时,输入数据框只包含“是”和“否”,我的代码失败。因此,我想要一个泛型代码来替换以下行:

代码语言:javascript
复制
table1 <- table(df1$Country, df1$State)

overview.df <- data.frame(Country = rownames(table1), 
                          Not.Delivered =table1[,1],
                          In.Transit = table1[,2],
                          Delivered = table1[,3])

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

EN

回答 1

Stack Overflow用户

发布于 2020-02-15 17:59:09

问题1:您需要使用fpar和/或block_list从R控制字体的大小、颜色等。默认情况下,使用的字体是模板中定义的字体。在您的示例中,您没有提供任何模板,因此使用默认模板。

问题2:也许可以使用flextable::proc_freq

代码语言:javascript
复制
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")

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60181026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档