Add row after click on button in row

Hi!

I am working on a shiny app where a user can edit a data table directly in the main panel. I have added a button in each row, and I want now that when the user clicks on a button in a certain row, underneath it a row appears with the same value for 'Time' as is the case in the row where the user clicks on the button. So if the user clicks on 'add row' in a row where the time value is 2 then the time value for the new row should be 2 as well. In this row, no action button should be added. If the user would like to add another row with time value 2, he should again click on the row that existed initially. If possible, the new row should now appear underneath the first row that he added instead of underneath the row with the button. So that I have the rows chronologically in the way the user added them.

I have now following code, but I do not know how I can make the button work correctly.

output$table <- renderDT({
rowsuser <- input$time
colstable <- c('Time','Amount', 'Action')
tableinitialisation <- c(1:rowsuser,rep(0,rowsuser), rep(NA, rowsuser))
data <- matrix(tableinitialisation, nrow=rowsuser,ncol=length(colstable))
colnames(data) <- colstable
data[, "Action"] <- sprintf(
'Add Row',
seq_len(rowsuser)
)
DT::datatable(data, editable = TRUE, escape=FALSE)
})

Could someone help me?

Hi @Marie321896

here an example if i understood what you want to do:

library(shiny)
library(DT)

shinyApp(ui <- fluidPage(DT::dataTableOutput(outputId = "yourDT")),
         
         server <- function(input, output) {
             df <- reactiveValues(
                 data = data.frame(
                     Amount = (1:5) * 10,
                     Time = 1:5,
                     Action = rep("Add row", 5),
                     stringsAsFactors = FALSE,
                     row.names = 1:5
                 ),
                 keepTrack = data.frame(id = 1:5,
                                        countClicked = rep(0, 5))
             )
             
             output$yourDT <- DT::renderDataTable(df$data,
                                                  selection = 'none',
                                                  options = list(pageLength = 50))
             
             observeEvent(input$yourDT_cell_clicked, {
                 info <- input$yourDT_cell_clicked
                 # do nothing if not clicked yet, or the clicked cell is not "Add row"
                 if (is.null(info$value) ||
                     info$value != "Add row")
                     return()
                 # get id
                 idSelected <-
                     which(df$keepTrack$id == df$data[info$row, "Time"])
                 # get position of new row
                 from <- info$row + df$keepTrack[idSelected, "countClicked"]
                 to <- length(df$data[, "Time"])
                 # make new row
                 rowToadd <- df$data[from,]
                 rowToadd[, "Action"] <- NA
                 # increase count
                 df$keepTrack[idSelected, "countClicked"] <-
                     df$keepTrack[idSelected, "countClicked"] + 1
                 # insert new row
                 if (from == to) {
                     df$data <- rbind(df$data, rowToadd)
                 } else{
                     df$data <-
                         rbind(df$data[1:from, ], rowToadd, df$data[(from + 1):to,])
                 }
             })
             
             
         })

Output:
R Shiny add row

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.