Hide column names in shiny table

2.6k views Asked by At

Is there a way to hide column names of a formattable? I thought about

  • changing an attribute in the formattable options. Didn't find something about it in the documentation or SO.
  • changing the font color to white for the header. I guess this may be an easy task for a CSS expert. I couldn't find the right sources to do it as a layman.

Maybe there is another option that I didn't think of? Thanks for your help in advance.

Example code below. The right table's header should be hidden.

library(shiny)
library(formattable)

df <- data.frame(A = LETTERS[1:10], B = 1:10)

server <- function(input, output) {
  output$table1 <- renderFormattable({
    formattable(df)
  })

  output$table2 <- renderFormattable({
    formattable(df)
  })
}

ui <- fluidPage(
  fluidRow(
    column(6,
      h6("Table with header"),
      formattableOutput("table1")
    ),
    column(6,
      h6("Table without header"),
      formattableOutput("table2")
    )
  )
)

shinyApp(ui = ui, server = server)

Additional: If there is a way to set cell borders like in Excel for the right table, solutions to this problem would also be appreciated.

2

There are 2 answers

1
Samuel On

Not exactly hiding, but here is my simple suggestion:

output$table2 <- renderFormattable({
  names(df) <- c("_", ".")
  formattable(df)
})

Any help to your problem?

2
sehock On

Add this to your code:

tags$head(tags$style(type = "text/css", "#table2 th {display:none;}"))

Note that you will need to manually set the widths of your columns as they will collapse to the smallest width without text overflowing to a new line.

What I've done here is used some CSS to tap into table2's properties. I access the header properties by declaring th after stating the table's ID. Any additional css for the header can go after the ;.