Hello -- in the following code, a checkbox is added to a dataframe. The ids selected are written to the box on the right.
When the application is fired up, the box is empty although all the boxes are checked. Click on any check box and the box has all the ids. This happens with the old datatable as well as the (new) datatable in DT.
Can you please help me with the JS code below so that the box shows all the ids which are checked right at the beginning, without an additional click? Thanks.
================app.R===========================
require(shiny)
require(DT)
server <- function(input, output, session) {
nrows <- 10
output$tbl <- DT::renderDataTable({
df <- data.frame(
varid=sample(10000000:20000000,nrows,replace=FALSE),
description=sapply(1:nrows, function(x) paste(sample(letters,10),collapse="")),
value1=round(runif(nrows),3), value2=round(runif(nrows),3)
)
addCheckboxButtons <- paste0('<input type="checkbox" name="row', df$varid, '" value="', df$varid, '" checked="1">',"")
DT::datatable(cbind(select=addCheckboxButtons, df), options = list(searching=FALSE, paging=FALSE, ordering=FALSE,
columnDefs=list(list(className='dt-center', targets=c(0,2)))),
callback = "function(table) {
table.on('change.dt', 'tr td input:checkbox', function() {
setTimeout(function () {
Shiny.onInputChange('varRows', $(this).add('tr td input:checkbox:checked').parent().siblings(':nth-child(2)').map(function() {
return $(this).text();
}).get())
}, 10);
});
}", escape=FALSE)
})
output$VarsSelected <- renderText({ paste(strwrap(paste(input$varRows, collapse=" "), width=35), collapse = '\n') })
}
ui <- shinyUI(
navbarPage("Example",
tabPanel("DataTable",
fluidRow(
column(offset=0, width=4,
DT::dataTableOutput('tbl')
),
column(offset=1, width=2,
h6("Selected"),
verbatimTextOutput("VarsSelected")
)
)
)
)
)
shinyApp(ui=ui, server=server)
===============================================