selectInput values lost after actionButton click

Using the following code I obtain the selection of each row for the column species_selector, however I see that only works the first time that I click on the "GENERATE DATA" button...

library(shiny)
library(DT)

ui <-
    tabItem("single_item_pricing2", class = "active",
            fluidRow(
                title = 'Selectinput column in a table',
                DT::dataTableOutput('foo'),
                verbatimTextOutput('sel'),
                textOutput("selected_var"),

                box(width = 12,
                    column(6,
                           actionButton("btnExecuteModel", label = "GENERATE DATA"),

                    ),
                )
            )
    )

server <- function(input, output, session) {
    data <- data.frame()

    observeEvent(input$btnExecuteModel, {
        data <<- head(iris, 5)


        for (i in 1:nrow(data)) {
            data$species_selector[i] <<- as.character(selectInput(paste0("sel", i), "", choices = unique(iris$Species), width = "100px"))
        }

        output$foo = DT::renderDataTable(data,
                                         escape = FALSE, selection = 'none', 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());")
                                                    )

        output$sel = renderPrint({
            str(sapply(1:nrow(data), function(i) input[[paste0("sel", i)]]))

        })

    })
}

shinyApp(ui, server)

Could someone explain to me how to solve this issue? Is like after the first button click, the value of (for example input[["sel1"]]) maintain just the original selection and doesn't update after.

Thanks in advance.


I think its an issue that each time the table renders its considered by javascript engine in your browser to be a new table, shiny is connected to the first. the unbindall doesnt work because its unbinding things on the new table and leaves the old table unaffected.

Here is a hacky solution, that coordinates the relationship between button, press and table creation,; a more elegant solution would be nice.

library(shiny)
library(DT)

ui <-
  fluidRow(
    title = "Selectinput column in a table",
    DT::dataTableOutput("foo"),
    verbatimTextOutput("sel"),
    textOutput("selected_var"),


        actionButton("btnExecuteModel", label = "GENERATE DATA"),
      
    )
  

server <- function(input, output, session) {
  data <- data.frame()
  
  observeEvent(input$btnExecuteModel, {
    data <- head(iris, 5)
    
    
    for (i in 1:nrow(data)) {
      data$species_selector[i] <- as.character(selectInput(paste0("sel", i,"_",input$btnExecuteModel), "", choices = unique(iris$Species), width = "100px"))
    }
    
    output$foo = DT::renderDataTable(data,
                                     escape = FALSE, selection = 'none', server = FALSE,
                                     options = list(dom = 't', paging = FALSE, ordering = FALSE),
                                     callback = JS("table.rows().every(function(i, tab, row) {
                                                        var $this = $(this.node());
                                                        console.log('this');
                                                        console.log(this);
                                                        $this.attr('id', this.data()[0]);
                                                        $this.addClass('shiny-input-container');
                                                      });
                                                      Shiny.bindAll(table.table().node());")
    )
    
    output$sel = renderPrint({
      print(paste0("sel", 1:nrow(data),"_",input$btnExecuteModel))
      
      str(sapply(1:nrow(data), function(i) input[[paste0("sel", i,"_",input$btnExecuteModel)]]))
      
    })
    
  })
}

shinyApp(ui, server)

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.