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)