Font in a gt table not working an in Rshiny app

Viewed 173

Font changes to a gt table are being reflected when we run the following code

    mtcars%>%
          head()%>%
          gt()%>%
          opt_table_font(
            font="Georgia"
  )

Table with fonts working properly Table1

However, when the same thing is run using an R shiny app, then the font are not reflected correctly

library(shiny)
library(gt)

ui <- fluidPage(
  gt_output("table")
)

server = function(input, output, session) {
  
  output$table <- gt::render_gt({

    data<-mtcars%>%
      head()%>%
      gt()%>%
      opt_table_font(
        font="Georgia"
      
  )

  } 
  )

}

shinyApp(ui, server)

enter image description here

1 Answers

Not an expert on this but as far as I get it the issue is that the CSS rule for the font family set by gt is overwritten by the default shiny css. One option to get your "Georgia" font is by adding some custom css to set the font family for gt tables like so:

library(shiny)
library(gt)

ui <- fluidPage(
  tags$head(
    tags$style(HTML("
      .gt_table {
        font-family: 'Georgia'
      }
    "))
  ),
  
  gt_output("table")
)

server = function(input, output, session) {
  
  output$table <- gt::render_gt({
    data<-mtcars%>%
      head()%>%
      gt()
  } 
  )
  
}

shinyApp(ui, server)
Related