0

I'm rendering a dataTable in a shiny application. 3 inputs will be entered by the user and on clicking the button, a dataTable will be rendered with 4 columns where column 1 will be uneditable, column 2 have a drop-down, column 3 has a date, and column 4 has a feature to delete a row if needed.

I have rendered a dataTable where column 2 has a drop-down. I am not clear on how to edit dates in the dataTable and also to delete a row.

The code which I used to implement is as follows:

library(shiny)
library(DT)

ui <- fluidPage(
  fluidRow(
    column(2, selectizeInput("type", "Type", choices = c("Item A","Item B","Item C","Item D"))),
    column(2, selectizeInput("freq", "Frequency", choices = c("Daily","Monthly","Weekly","Twice Weekly"))),
    column(2, dateInput("typeDate", "Date", value = NA)),
    column(3, actionButton("addRow", "Add")),
    column(3, actionButton("overallDates","Overall Summary"))
  ),
  fluidRow(
    column(12, 
           DT::dataTableOutput("tableData"))
  )
)

server <- function(input, output, session){
  combinedData <- data.frame()
  observeEvent(input$addRow,{
    if(!is.null(input$type) & !is.null(input$freq) & !is.null(input$typeDate)){
    typeData <- input$type
    typefreq <- input$freq
    dateType <- input$typeDate
    
    individualData <- data.frame("Type" = typeData, "Frequency" = typefreq, "Date" = dateType, stringsAsFactors = FALSE) 
    combinedData <<- rbind(combinedData, individualData)
    for (i in 1:nrow(combinedData)) {
      choiceValue <- combinedData$Frequency[i]
      dateValue <- combinedData$Date[i]
      combinedData$Frequency[i] <- as.character(selectInput(paste0("sel",i),"", choices = c("Daily","Monthly","Weekly","Twice Weekly"),selected = choiceValue, width = "100px"))
      #combinedData$Date[i] <- dateInput(paste0("datesel",i),"", value = NULL)
    }
    
    output$tableData <- renderDataTable(
      combinedData, selection = 'none', editable = TRUE, escape = FALSE,
      server = FALSE, options = list(dom = 't', paging = FALSE, ordering = FALSE),
      callback = JS("table.rows().every(function(i, tab, row) {
        var $this = $(this.node());
        $this.attr('id', this.data()[0]);
        $this.addClass('shiny-input-container');
      });
      Shiny.unbindAll(table.table().node());
      Shiny.bindAll(table.table().node());")
      
    )
    observeEvent(input$deliverableData_cell_edit,{
      combinedData[input$deliverableData_cell_edit$row, input$deliverableData_cell_edit$col] <<- input$deliverableData_cell_edit$value
    })
    
    print(combinedData)
    
    }
    
  })
  
  
}

shinyApp(ui = ui, server = server)

Can anyone provide a solution of how to add dateInput in one column and delete a row if needed by having a delete feature at the last column of the dataTable?

Nevedha Ayyanar
  • 845
  • 9
  • 27

0 Answers0