0

I have an editable datatable in R shiny with user names and start/end dates. I want to add a second header above the main header, and then add a drop down filter based on the top header. How do I go about adding the top row header and linking it to the datatable?

Code:

library(shiny)
library(shinydashboard)
library(tidyverse)
library(DT)

header <- dashboardHeader(title = "My First App")
sidebar <- dashboardSidebar()

body <- dashboardBody(
  tabItems(
    tabItem(
      tabName = 'User View', class = 'active', 
      fluidRow(
        box(
          dataTableOutput('userTable'), width = 6
        )
      )
    )
  )
)


ui <- dashboardPage(title = 'User View', header, sidebar, body, skin='blue')

server <- function(input, output, session){
  
  dat <- data.frame(name = c("John","Mary","Mike","JJ","Alex","Frido"), start = c("06/08/2011","08/11/2014","13/07/2000","1/1/2006","4/6/2002","9/8/1999"), end = c("06/08/2011","08/11/2014","13/07/2000","1/1/2006","4/6/2002","9/8/1999"),stringsAsFactors = FALSE)
  
  output$userTable <- renderDataTable({
    DT::datatable(isolate(dat),
                  editable = TRUE,
                  rownames = FALSE,
                  options=list(
                    searching = FALSE,
                    paging = FALSE
                  ))
  })
  
  ###Tracking Changes###
  rvs <- reactiveValues(
    data = NA #dynamic data object
  )
  
  observe({
    rvs$data <- dat
  })
  
  proxy = dataTableProxy('userTable')
  observe({
    DT::replaceData(proxy, rvs$data, rownames = FALSE, resetPaging = FALSE)
  })
  
  observeEvent(input$userTable_cell_edit, {
    rvs$data <<- editData(rvs$data, input$userTable_cell_edit, rownames = FALSE)
  })
  
}

shinyApp(ui = ui, server = server)

Current Output:

enter image description here

Desired output:

enter image description here

dm2111
  • 135
  • 1
  • 6
  • My guess is that `datatable` does not have that feature. The [gt](https://gt.rstudio.com/) package does though. But you'll lose the interactive features `datatable` has. If this table displays results and does not need pagination, filter, sorting etc., `gt` would be a good alternative. – Till Mar 07 '22 at 21:36
  • 3
    Consider this answer: https://stackoverflow.com/a/45934505/10266675 – Johan Rosa Mar 07 '22 at 22:05
  • 1
    Does this answer your question? [How to create datatable with complex header in R Shiny?](https://stackoverflow.com/questions/22897852/how-to-create-datatable-with-complex-header-in-r-shiny) – Mikko Marttila Mar 08 '22 at 13:55

0 Answers0