Когда я добавляю новую строку с динамическими входами в блестящий набор данных, предыдущая строка ввода теряет реактивность в R - PullRequest
0 голосов
/ 14 сентября 2018

проверяет наличие элемента реактивных значений при добавлении, чтобы он содержал вход для флажка, а затем, как отражать текущее значение флажка, а не первое выбранное по умолчанию значение

shinyApp(
    ui = fluidPage(dataTableOutput('foo'), actionButton("save_changes","add")),
    #server side computing server = function(input, output) {
        # Collection configuration table
        # create a character vector of shinyinputs
        shinyInputadd = function(FUN, len, id, labl ,...) {
            iter = character(len) inputs = as.character(FUN(paste0(id, len), label = labl, ...)) inputs
        }
        rowcount <- reactiveValues(cnt = 0) 
        output$foo <- DT::renderDataTable({
            DT=bvals$data DT$ID <- NULL datatable(
                DT,
                escape=F,
                selection = 'none',
                rownames = FALSE,
                options = list(
                    pageLength = 20,
                    autoWidth = FALSE,
                    scrollX = FALSE,
                    scrollY = '500px',
                    dom = 't',
                    ordering = FALSE,
                    preDrawCallback = JS('function() {
                        Shiny.unbindAll(this.api().table().node());
                    }'),
                    drawCallback = JS('function() {
                        Shiny.bindAll(this.api().table().node());
                    }')
                )
            )
        })
        bvals <- reactiveValues(
            data = data.frame(
                "ID" = "None",
                "SELECT" = "None",
                "SEGMENT" = "None",
                "SCENARIO" = "None",
                "REMOVE" = "None"
            )[0,]
        )
        #when user clicks 'Add '
        observeEvent(
            input$save_changes,
            {
                rowcount$cnt <- rowcount$cnt + 1 n <- rowcount$cnt
                #remove rows with value None
                batchdt = bvals$data dt_sel_id <- data.frame(
                    "SELECT" = shinyInputadd(
                        checkboxInput,
                        n,
                        "bcheckb_",
                        "",
                        value = TRUE
                    ),
                    "ID" = n
                )
                #Create a dataframe with one row having Delete button
                dt_rem_button <- data.frame(
                    "REMOVE" = shinyInputadd(
                        actionButton,
                        n,
                        "bdelete_",
                        "",
                        align = "right",
                        class ="needed",
                        icon("minus-circle"),
                        style="color: #b22222; background-color: #fff ;height = 70px ;width = 70px"
                    )
                )
                dt_bind <- cbind(
                    dt_sel_id,
                    data.frame(
                        "SEGMENT" = 1,
                        "SCENARIO" = 1
                    ),
                    dt_rem_button
                )
                #Append new row with new seqUence and the above buttons to the existing values in the collection table
                bvals$data <- rbind(
                    bvals$data,
                    dt_bind
                )
            }
        )
    }
)
...