1

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.

ruser
  • 199
  • 12
  • 1
    https://stackoverflow.com/questions/50726187/how-can-i-change-in-r-the-font-family-of-a-title-with-officer – shome Feb 12 '20 at 05:01
  • @shome thanks for the link; but it does not mention how I can reduce the font size of the data frame which I want exported to slide. can you help me with sample code please? – ruser Feb 12 '20 at 05:42
  • 1
    @ruser..Check the first answer for font related settings.I am trying to put some code. – shome Feb 12 '20 at 08:46

1 Answers1

2

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

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34