datatable and checkbox

2,324 views
Skip to first unread message

ShinyIsCool

unread,
Feb 7, 2015, 12:58:54 AM2/7/15
to shiny-...@googlegroups.com

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)
===============================================

ShinyIsCool

unread,
Feb 7, 2015, 10:08:47 AM2/7/15
to shiny-...@googlegroups.com
Is there a different way of adding a checkbox in the new DT which would avoid this refresh issue?  Thanks.

Yihui Xie

unread,
Mar 11, 2015, 8:42:07 AM3/11/15
to ShinyIsCool, shiny-discuss
Just for the record: http://rstudio.github.io/DT/shiny.html

Regards,
Yihui

ShinyIsCool

unread,
Mar 11, 2015, 8:48:04 AM3/11/15
to shiny-...@googlegroups.com, vijay...@gmail.com
Thank you!  How would I change this so that the default option would be that all the checkboxes are checked?

Yihui Xie

unread,
Mar 11, 2015, 9:04:15 AM3/11/15
to ShinyIsCool, shiny-discuss
Please install the DT package and read the documentation ?DT::checkboxRows

Regards,
Yihui
> --
> You received this message because you are subscribed to the Google Groups
> "Shiny - Web Framework for R" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to shiny-discus...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/shiny-discuss/cba1d3de-1ca9-4354-bb62-c952bc654a9e%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

ShinyIsCool

unread,
Jun 4, 2015, 12:35:00 AM6/4/15
to shiny-...@googlegroups.com, vijay...@gmail.com
Has the function checkboxRows been removed from DT?  I updated to the latest version of DT just now and I get the following error "checkboxRows is not an exported object from namespace DT"

ShinyIsCool

unread,
Jun 4, 2015, 1:05:16 AM6/4/15
to shiny-...@googlegroups.com
That does seem to be the case based on the response to this post  -- http://stackoverflow.com/questions/30529027/display-of-checkboxes-in-datatable-in-shiny-not-displaying-html-code-displays

How can this be written so that the initial setting would have all the rows selected?
Also is there an easy way to change the selection color (make it lighter)?

In my app, I am using this to select variables for a Lasso regression.  By default I would like all the variables to be selected but the user may want to unselect some before running the algorithms.

Thanks.

ShinyIsCool

unread,
Jun 4, 2015, 9:24:49 AM6/4/15
to shiny-...@googlegroups.com
I implemented so that clicking on a row means the variable is dropped. By default all the variables are in the regression model.

Yihui Xie

unread,
Jul 19, 2015, 10:36:55 PM7/19/15
to ShinyIsCool, shiny-discuss
Yes, this function has been removed. See
http://rstudio.github.io/DT/shiny.html for more information on
row/column selections.

To customize the selection color, you may use CSS. The default
selection color is defined here:

https://github.com/rstudio/DT/blob/50a84275/inst/htmlwidgets/lib/datatables/css/default/dataTables.extra.css#L16-L25

To learn how to customize CSS, please see this article:
http://shiny.rstudio.com/articles/css.html

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages