I have a question that has been somehow asked in the past but not exactly in the way I need. I have the following R dataframe:
df <- data.frame(Identifier=c(1,2,3,4), STATE=c('NY','CA','TX','FL'), STATE_NAME=c("New York","California","Texas","Florida"),CRIME_RATE=c(0.2, 0.3, 0.35, 0.4), EMPLOYMENT=c(0.8,0.8,0.7,0.5))
Now, I would need to display the dataframe like this:
I read https://rstudio.github.io/DT/ section 2.6 however, the example there doesn't have multiple rows for each individual column header.
Same problem here: Center custom data table container column headers in Shiny
I found solution Rstudio shiny renderDataTable headers multi line? interesting in the sense that perhaps using html
could have allowed me to use one single column header but displayed over multiple rows, however it doesn't seem to work.
This is my output code. Notice I use extensions = "Buttons", because the actual dataframe is way bigger and this allows users to export the data to csv and excel.
Thanks
output$output_table <- renderDataTable({
df <- data.frame(Identifier=c(1,2,3,4), STATE=c(NY,CA,TX,FL), STATE_NAME=c("New York","California","Texas","Florida"),CRIME_RATE=c(0.2, 0.3, 0.35, 0.4), EMPLOYMENT=c(0.8,0.8,0.7,0.5))
df <- datatable(df,
rownames= F,
filter = 'top',
extensions = "Buttons",
options = list(scrollX = TRUE
, autoWidth = TRUE
, pageLength = 66
, dom = 'Blfrtip'
,buttons = c('copy', 'csv', 'excel', 'pdf')
))
return(df)
})