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

using checkboxes: how to avoid a race? #35

Closed
smartinsightsfromdata opened this issue Mar 31, 2015 · 10 comments
Closed

using checkboxes: how to avoid a race? #35

smartinsightsfromdata opened this issue Mar 31, 2015 · 10 comments

Comments

@smartinsightsfromdata
Copy link

If you click very fast on one checkbox, the datatable and the resulting slider enter in an endless loop. It happens also in my app.
I have built the code below (apologies for the length) to reproduce it.

What could I do to avoid this??

library(DT)
library(shiny)

quantity <- id <- 1:nrow(iris)
label <- paste0("lab","-",quantity)
iris_ <- cbind(id=id,quantity=quantity,label=label,iris ,stringsAsFactors = FALSE)
cols <- colnames(iris_)

ui <- fluidPage(
    fluidRow(
        column(8,DT::dataTableOutput('demTb')),
        column(3,uiOutput("demSli"))
        )
    )

server <- function(session, input, output) {


output$demSli <- renderUI({

if(is.null(input$demTb_selected) ) return()

isolate({
index <- input$demTb_selected

labs <- iris_$label[index] 
pages <- "test"
iter <- length(labs)
buttn <- 1
valLabs <- sapply(1:iter, function(i) {
if(is.null(input[[paste0(pages,"d",labs[i],buttn)]] )) {
          0
} else {  as.numeric(input[[paste0(pages,"d",labs[i],buttn)]])  }
}) 
#
toRender <- lapply(1:iter, function(i) {
  sliderInput(inputId = paste0(pages,"d",labs[i],buttn),
              label =  h6(paste0(labs[i],"")),
              min = -100,
              max = 100,
              step = 1,
              value = valLabs[i],
              post="%",
              ticks = FALSE, animate = FALSE)
              })
})
      return(toRender)

})
#
rds <- reactive({
if( is.null(input$demTb_selected) ) return(iris_)
index <- input$demTb_selected
isolate({
labs <- iris_$label[index] 
pages <- "test"
iter <- length(labs)
buttn <- 1
}) # end isolate
valLabs <- sapply(1:iter, function(i) {
    if(is.null(input[[paste0(pages,"d",labs[i],buttn)]] )) {
      0
    } else {  
      as.numeric(input[[paste0(pages,"d",labs[i],buttn)]])/100  
    }
  })

  df_ <- data.frame(label=labs, multi=valLabs, stringsAsFactors = FALSE)
  df_ <- merge(iris_,df_,by="label", all.x=T)
  ifelse( is.na( df_$multi), df_$quantity, df_$quantity*(1 + df_$multi) )
  df_$multi <- NULL
  return(df_)
  }) 


output$demTb = DT::renderDataTable({


if(is.null(rds() )) return()
df_ <- rds() 
df_ <- df_[cols]
df_ <- df_[with(df_,order(as.numeric(id))),]

DT::datatable(df_,extensions= 'Scroller', rownames = checkboxRows(df_, input$demTb_selected),
      escape = -1,
      options=list(
      # deferRender = TRUE,
      dom = 'frtiS',
      scrollX = TRUE,
      scrollY = 300,
      scrollCollapse = TRUE,
      searching=F
      ))

    })

}


shinyApp(ui, server)
@yihui
Copy link
Member

yihui commented Mar 31, 2015

Sorry I tried my best to click as fast as I can, but I just could not reproduce the problem. I'm not sure if it is because my laptop is just too fast... (8G Memory, Intel Core i7-3630QM CPU @ 2.40GHz × 8).

@smartinsightsfromdata
Copy link
Author

(Sadly) I can do it every time. Not sure how much depends from the actual speed of the device.
With my Macbook pro (16GB, 3 GHz Intel Core i7, 2 cores) and the apple trackpad is very easy to reproduce (in this case because it is very easy to click very fast on the trackpad).
It happens (more sadly!) in my application based on shiny pro (on centos 6.5) and a remote client. I believe a slow connection between server and client makes it possible to generate a disconnect (different statuses cached etc.).
Could you kindly try selecting many boxes, then very quickly selecting / deselecting a middle box?

Also, would it help if I take a screencast?

@smartinsightsfromdata
Copy link
Author

@yihui I have done a screen recording of the problem, but it is not supported by github.

Here is the video https://www.dropbox.com/l/tpzeafZeyB2JOas3PrusKs/invite
or
https://www.dropbox.com/sh/eevmfrahay9bq3s/AADu5qSWu81OAlVISSTndTCva?dl=0

@yihui
Copy link
Member

yihui commented Mar 31, 2015

Thanks for the screen recording. I can see your problem.

Perhaps it was a bad idea from the beginning when I told you the checkboxRows(df_, input$demTb_selected) trick, since it means the table can update itself when you click the checkboxes. Then the renderUI() stuff makes the dependency structure even more complicated... Every time I have to stop and think for a while to figure out what this app really does. Sorry, I guess I don't have the bandwidth at the moment to solve this problem. I'll see if I can try again a few days later.

@smartinsightsfromdata
Copy link
Author

@yihui As I have a similar problem with shiny and numericInput (and renderUI), I've just posted an issue on the shiny site referencing this one as well.

@yihui
Copy link
Member

yihui commented Apr 1, 2015

Thanks! You may also post an example of numericInput() and renderUI() there. I think that example should be easier to debug than this DT one.

@smartinsightsfromdata
Copy link
Author

@yihui so far nothing has happened on this issue:

Would you kindly have time to help?

@yihui
Copy link
Member

yihui commented Apr 14, 2015

I guess this issue is not specific to DT, but is a general shiny problem, so I'm closing this issue, and let's keep the discussion in the shiny repo only. Anyway, as Joe pointed out, if only you could avoid the circular dependency...

@yihui yihui closed this as completed Apr 14, 2015
@jasdumas
Copy link

Is the rownames = checkboxRows() still active ? This would be a useful feature in appending checkboxes to the rows

@yihui
Copy link
Member

yihui commented Jun 17, 2015

@jasdumas No, it has been removed. Please see #93 (comment)

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

No branches or pull requests

3 participants