I need help in displaying, in shiny, a table that has been formatted using the gt() function. In my reprex below the table is displayed, but without the formatting. Thanks in advance.
Philip
library(shiny)
library(gt)
a <- c(3,6,4,8,6)
b <- c(10,13,20,26,26)
c <- c(1,2,3,4,5)
df <- data.frame(a=a,b=b,c=c)
gtTable <- function(df,titl) {
gt_tbl <- gt(df)
gt_tbl <- tab_options(gt_tbl,table.font.size=11,
container.width = 450,
table.background.color="bisque",
heading.background.color="bisque")
gt_tbl <- tab_header(gt_tbl,
title=md(html(paste0("**",tbl_nam,"**"))))
gt_tbl <- cols_align(gt_tbl,
align=c("left"),
columns=vars(a))
gt_tbl <- cols_label(gt_tbl,
a="",
b=md("**b**"),
c=md("**c**"))
gt_tbl <- fmt_number(gt_tbl,
columns=vars(a,b,c),
decimals=1,
use_seps=TRUE)
tbl <- gt_tbl
}
ui <- fluidPage(
selectInput("dataframe", "Choose a table",choices = "df"),
selectInput("tab_nam", "Choose a table name",choices = "MyTitle"),
tableOutput("tab")
)
server <- function(input,output,session) {
tabl <- reactive({get(input$dataframe)})
tabl_nam <- reactive({input$tab_nam})
#output$tab <- renderTable(tabl()) # renders an unformatted table correctly
tbl <- reactive({gtTable(tabl(),tabl_nam())})
output$tab <- renderTable(tbl())
}
shinyApp(ui,server)