'edit a data table depending on selectInput, keep the information filled in by the user and display a complete table - DT package

I'm currently working with Shiny and I have issues with a datatable:

  • When I make a choice on the selectInput, I would like it to show me the corresponding table row
  • When I modify a cell of the table, I would like it to take it into account
  • I would like to display a final table that has taken all the changes into account

You will find below a reproducible code (ui and server in 2-files) for better visualization of what I am talking about.

ui = dashboardPage(skin="red", 
  dashboardHeader(title="Test"),
    dashboardSidebar(
      sidebarMenu(
        menuItem("Issue", tabName = "readData",icon = icon("folder-open"))
      )
    ),
    dashboardBody(
      tabItems(
      # Case study configuration
        tabItem(tabName = "readData",
          h2("Specifications"),
          selectInput(inputId = "Resp", label = "Response choice", choice = c("Y1", "Y2"), selected ="Y1"),
          radioButtons(inputId = "Obj", label= "Response objective", 
                       choices = c("Maximize","Minimize","Achieve a target"),inline=T),
          textOutput("respobj"),
          DTOutput("spec"),
          br(), #saut de ligne
          DTOutput("specOK"),
          actionButton("saveBtn","Save") 
        )
      )
  )
)

shinyServer(function(input, output, session){
  
  updateSelectInput(session, "Resp", label = "Response choice", choices = colnames(iris), selected = character(0))
  
  #Tableau des spec avec 1 par defaut non modifiable
  output$spec = renderDT(mat_spec, options = list(dom = 't'), editable = FALSE)
  resp = reactive({
    numj = match(input$Resp,colnames(iris))
  })
  #  output$spec = renderText(print(numj))
  mat_spec = matrix(rep(1), nrow=ncol(iris), ncol=5)
  rownames(mat_spec) = colnames(iris)
  colnames(mat_spec) = c("Lower limit", "Target value", "Upper limit", "s coeff", "t coeff")
  
  output$respobj = renderPrint({
    if (input$Obj=="Maximize") output$spec = renderDT(mat_spec[1,,drop=FALSE], options = list(dom = 't'), editable = list(target="row", disable = list(columns = 3)))
    if (input$Obj=="Minimize") output$spec = renderDT(mat_spec[2,,drop=FALSE], options = list(dom = 't'), editable = list(target="row", disable = list(columns = 1)))
    if (input$Obj=="Achieve a target") output$spec = renderDT(mat_spec[3,,drop=FALSE], options = list(dom = 't'), editable = list(target="row"))
  })
  
}
)

Thank in advance for your help,

Diane



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source