Putting formulas into a PowerPoint generated by Shiny

Hello!

I am trying to add formulas into PowerPoint slides from Shiny. However, the formulas do not "expand", but rather go to the next line.

Here is my code:

library(shiny)
library(officer)
library(flextable)
library(dplyr)
my_table <- data.frame(
  Name = letters[1:4],
  Age = seq(20, 26, 2),
  Occupation = LETTERS[15:18],
  Income = c(50000, 20000, 30000, 45000)
)
    
ui <- fluidRow(
  column(
    width = 12,
    align = "center",
    tableOutput("data"),
    br(),
    downloadButton("download_powerpoint", "Download Data to PowerPoint")
  )
)
    
server <- function(input, output) {
library(flextable)
library(officer)
library(dplyr)
library(latex2exp)


  output$data <- renderTable({
    my_table
  })
  
  output$download_powerpoint <- downloadHandler(
    filename = function() {  
      "employee_data.pptx"
    },
    content = function(file) {
      flextable_prep <- flextable(my_table) %>% 
        colformat_num(col_keys = c("Age", "Income"), digits = 0) %>% 
        width(width = 1.25) %>% 
        height_all(height = 0.35) %>% 
        theme_zebra() %>% 
        align(align = "center", part = "all")
      
      example_pp <- read_pptx()  
        example_pp <-add_slide(example_pp,layout = "Title Slide",
	master = "Office Theme")
	print("first slide")
        example_pp <- ph_with(x=example_pp,
          location=ph_location_type(type="ctrTitle"),
          value = "Employee Data"
        )
	print("first ph_with")
        example_pp <- ph_with(x=example_pp,
          location = ph_location_type(type = "subTitle"),
          value = "Company 2019 Report"
        )
	print("second ph_with")
	xmj <-c(
  	    "(ax^2 + bx + c = 0)",
  	    "a \\ne 0",	"x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}")
	df1 <- data.frame(formula=xmj)
	ft1 <- flextable(df1)
	ft1 <- compose(
	  x = ft1,j="formula",
	  value=as_paragraph(as_equation(formula)))
	ft1 <- align(ft1,align="center",part="all")
#	ft1 <- width(ft1,width=5)
        example_pp <- add_slide(example_pp,layout = "Title and Content",
	master = "Office Theme")
        example_pp <- ph_with(x=example_pp,
          location=ph_location_type(type="body"),
          value = "Test"
	  )
	print("third ph_with")
	table_width <- sum(dim(ft1)$widths)
	table_height <- sum(dim(ft1)$heights)
	print(table_width)
	print(table_height)
	slide_width <- 10
	slide_height <- 7.5
	table_leftmargin <- (slide_width-table_width)/2
	table_topmargin <- (slide_height-table_height)/2
	print("left")
	print(table_leftmargin)
        example_pp <- ph_with(x=example_pp,
          value = ft1,
#	  location=ph_location(left=table_leftmargin,
#	  top=table_topmargin)
          location=ph_location_type(type="body")
        )
	example_pp <- set_notes(x=example_pp,
	value="Correct Answer",
	location=notes_location_type("body")
	)
      
      print(example_pp, target = file)
    }
  )
}
      
shinyApp(ui, server)

And here is the "bad" slide:
image

Any suggestions, please?

Thank you so much!
Erin

Hi Erin! From what I can tell, it appears the table width may be causing the issue. When ft1 is first created with no width specified, the content wraps.

ft1 <- flextable(df1)
image

When a column width is specified, the full equations are spread out.
ft1 <- flextable(df1, cwidth = 3)
image

For some reason, when I try to run the app, I can't get the equations to show in the downloaded .pptx, but the change above does make the table wider. I can't tell for sure on the final output, but this is one thing you can try.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.