简体   繁体   中英

Regarding "officer" package and Table function in R

I have the following piece of code where I: 1) populate a data frame and its summary into PPT using officer package 2) Calculate summary from data frame using table function

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

I have 2 questions here which I am unable to solve and need help:

1) I am unable to change the font and font size of the output in PPT. Can someone help me here 2) When I calculate the dataframe "overview.df" from "table1", sometimes the output contains "Yes, "No" and "Transit" for which the above code works. But sometimes, the input data frame only contains "Yes" and "No" for which my code fails. Hence I would like a generic code to replace the following lines:

table1 <- table(df1$Country, df1$State)

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

Thanks all in advance! Awaiting your reply.

Question 1: you need to use fpar and/or block_list to control from R the size, color, etc. of fonts. By default, the font used is the font defined in your template. In your case, you don't provide any template so the default one is used.

Question 2: maybe use 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")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM