闪亮:不能删除行超过 1 次

Shiny: cannot delete rows more than 1 time

我正在开发一款闪亮的应用程序。 server.R 包含类似......

的代码
dFt1 <- reactiveValues()
dFt1$dat <- data.frame(loadTransactionData())
...
output$t_tab_1 <- DT::renderDataTable({
    DT::datatable(
        dFt1$dat
        ,selection = list(mode = "single")
        ,options = list(
            rownames = TRUE
            ,pageLength = 10
            ,order = list(list(2,"desc"),list(1,"asc"))
        )
    )
})
...
observe({
    if (is.null(input$delete) || input$delete == 0){return()}
    session$sendCustomMessage(
        type = 'jsCode'
        ,list(value = 'confirm("Are You Sure?");')
    )
})

observeEvent(input$deleteConfirmChoice, {
    if (input$deleteConfirmChoice == "TRUE") {
        x <- input$t_tab_1_rows_selected
        deleteTransaction(x)
        isolate(dFt1$dat <- dFt1$dat[row.names(dFt1$dat) != x, ])
    }
})  

deleteTransaction <- function(x) {
    qy <- "DELETE FROM Transactions where timestamp = '<t>'"
    qy <- gsub("<t>",x,qy)
    db <- dbConnect(SQLite(), dbname=systemDatabase)
    dbGetQuery(db,qy)
    dbDisconnect(db)
}

当然 ui.R 中有删除按钮和调用 javascript 确认框。

应用程序运行良好。我可以 select 记录并删除它。然后我可以 select 另一个记录。但我不能删除它。 javascript 确认框运行,但单击是不会删除记录。不知道为什么1次可以,下次就不行

有什么帮助吗?

好的!终于找到有用的东西了...

    observeEvent(input$deleteConfirmChoice, {
    if (input$deleteConfirmChoice == "TRUE") {
        x <- input$t_tab_1_rows_selected
        deleteTransaction(x)
        isolate(dFt1$dat <- dFt1$dat[row.names(dFt1$dat) != x, ])
        shinyjs::disable("delete")
        shinyjs::disable("edit")
        session$sendCustomMessage(
            type = 'jsCode'
            ,list(value = '1 != 1;')
        )
    }
})  

我添加了另一个评估为 FALSE 的 session$sendCustomMessage 调用。

请问有没有更好的解决办法?

这是另一种方法...

我更改了传递给 session$sendCustomMessage 的 javascript 代码。单击“否”按钮 returns 0。单击“是”按钮 returns 一个随机数。我希望这将保证在连续两次单击“是”按钮时得到不同的值。

    observe({
    if (is.null(input$delete) || input$delete == 0){return()}
    session$sendCustomMessage(
        type = 'jsCode'
        ,list(value = 
            '
            (function() {
                if (confirm("Are you sure?")) {
                    return Math.random()*3 + 1;
                } else {
                    return 0;
                }
            })()
            '
        )
    )
})

observeEvent(input$deleteConfirmChoice, {
    if (input$deleteConfirmChoice == "TRUE") {
        x <- input$t_tab_1_rows_selected
        deleteTransaction(x)
        isolate(dFt1$dat <- dFt1$dat[row.names(dFt1$dat) != x, ])
    }
})  

还有其他更清洁的解决方案吗?