Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(enhancement) add checkbox to DT #20

Closed
smartinsightsfromdata opened this issue Mar 5, 2015 · 11 comments
Closed

(enhancement) add checkbox to DT #20

smartinsightsfromdata opened this issue Mar 5, 2015 · 11 comments

Comments

@smartinsightsfromdata
Copy link

@yihui It would be great to add checkbox on datatables / DT.
I've toyed a bit with this on earlier version and I found the following issues, that would require addressing in view of a larger more general adoption:

  • In the old datatables a "ticked" check box didn't survive pagination or sorting: at the very least it is important to have means to "save" server-side the info on which checkboxes have been selected before allowing paging or sorting (or allow checkboxes only when 'Scroller" is T & pagination is F.
  • As a way to manage the info from the boxes this should be implemented alongside the indexing strategy (as discussed here)
  • Full abillity to set a checkbox on or off from server-side
  • In an ideal world checkboxes on columns as well as on rows (through lists?)
@yihui yihui closed this as completed in eda2335 Mar 9, 2015
@yihui
Copy link
Member

yihui commented Mar 9, 2015

Please see http://rstudio.github.io/DT/shiny.html and let me know if there is anything that you want me to improve or change. Thanks!

@smartinsightsfromdata
Copy link
Author

@yihui Perhaps I'm missing something, but I am not able to make this work for me.
Let me clarify. As you'll see from the code below, I intend to use the checkbox to alter the table itself.

Sadly this nullify the checkbox: as soon as the new value is entered the checkbox is lost / not-persisted: this means for example that I cannot use sliders etc.

Is there a way to update the datatable preserving the checkbox?
Here is the code:

library(shiny)
library(DT)


df_ <- data.frame(col1 = as.numeric(11:22), col2 = 1:12,
                  col3 = 31:42, mth = month.abb, 
                   stringsAsFactors = F) 


runApp(list( 
  ui = fluidPage(
    fluidRow( 
      column(1, actionButton("loadButtn", label="Load")),
      column(6, DT::dataTableOutput('dtTb')),
      column(3, uiOutput("outSel")) 
    )                        
  ), 
server = function(input, output, session) {

output$dtTb <- DT::renderDataTable({
  if(input$loadButtn ==0) return() 
  if(! is.null(input$dtTb_selected)) {
  isolate({
  index <- input$dtTb_selected 
  picked <- month.abb[index]
  loop <- length(picked)
  buttn <- 1
  }) # end isolate
  vals <- sapply(1:loop, function(i) {
    if(is.null(input[[paste0(picked[i],buttn)]] )) {
      0
    } else {  
      as.numeric(input[[paste0(picked[i],buttn)]])  
    }
  }) 
  df_$col2[which(df_$mth %in% picked)]  <- vals
  }
  DT::datatable(df_, rownames = checkboxRows(df_),
                escape = -1,
                options=list(
                  deferRender = TRUE,
                  autoWidth = FALSE,
                  searching=F
                ))
    })


####

  output$outSel <- renderUI({
      if (length(input$dtTb_selected)==0) return() 
      index <- input$dtTb_selected
      picked <- month.abb[index]
      buttn <- 1
      loop <- length(picked)
      valsTv <- sapply(1:loop, function(i) {
        if(is.null(input[[paste0(picked[i],buttn)]])) {
          0
        } else {
          as.numeric(input[[paste0(picked[i],buttn)]])
        }
      })
      toRender <- lapply(1:loop, function(i) {
      sliderInput(inputId = paste0(picked[i],buttn),
                    label =  h6(paste0(picked[i],"")),
                    min =  0,
                    max = 10,
                    step = 1,
                    value = as.numeric(valsTv[i]) ,
                    #post="%",
                    ticks = FALSE, animate = FALSE)
      })
      return(toRender) 
    })
  }
))

@yihui
Copy link
Member

yihui commented Mar 10, 2015

If you change the data in a table, the table will be re-rendered, and datatable() knows nothing about the relationship between the new data and the old data, so it may not make sense to preserve the previous selections by default. Anyway, I just added a new argument checked in checkboxRows(), and you can use checkboxRows(df_, input$dtTb_selected) to preserve previous selections. Please note you should not use deferRender = TRUE, though. The checkboxes should be rendered immediately and not deferred.

@smartinsightsfromdata
Copy link
Author

@yihui it works like a charm! Thanks.

@yihui
Copy link
Member

yihui commented Mar 10, 2015

Glad to know that :) It is not very efficient, though, to redraw the whole table every time you check/uncheck a row in the table. It may not be a real issue when the data is small. I did not test substantially larger data objects.

@smartinsightsfromdata
Copy link
Author

@yihui I agree this method does not scale well to large amount of data. In principle one could a) cache the table each time; b) calculate the deltas for each update; c) send only the deltas after the first time (taking into account issues like partials etc.). But it is quite complex to do it right within a reactive framework. I'm still looking for someone to provide the magic...

@frajuegies
Copy link

Dear all,
is the use of 'rownames = checkboxRows limited to server=FALSE ?
Thank you very much for any hint
Jürgen

@yihui
Copy link
Member

yihui commented May 23, 2015

@frajuegies Currently, yes, but I have been thinking about what to do for server = TRUE.

@frajuegies
Copy link

Great...
Best regards,
Jürgen

@jasdumas
Copy link

The https://yihui.shinyapps.io/DT-checkbox/ link returns a 404 error. Any fix or is the example available at a different location?

@yihui
Copy link
Member

yihui commented May 31, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants