Automatically updating reactive values based on user numeric input

1,043 views
Skip to first unread message

Matthew Brown

unread,
Feb 21, 2017, 10:24:14 AM2/21/17
to Shiny - Web Framework for R
I'm trying to create a column in a data frame which accepts user input and automatically updates reactive values. I'm able to create the column and the values automatically update but my issue is that I also need to allow users to delete rows from the data frame. When I delete rows from the data frame it breaks. My reproducible example is below.

#Simple example
library(shiny)
library(data.table)
library(DT)
library(shinyjs)
library(shinythemes)
library(lpSolve)


server <- function(input, output,session) {
shinyInput <- function(FUN, len, id, ...) {
  inputs <- character(len)
  for (i in seq_len(len)) {
    inputs[i] <- as.character(FUN(paste0(id, i), ...))
  }
  inputs
}

shinyInput2 = function(FUN, len, id, val, ...) { 
  inputs = character(len) 
  for (i in seq_len(len)) { 
    inputs[i] = as.character(FUN(paste0(id, i), label = NULL,value=round(as.numeric(val[i]),1), ...)) 
  } 
  inputs 

# obtain the values of inputs 
shinyValue = function(id, len) { 
  unlist(lapply(seq_len(len), function(i) { 
    value = input[[paste0(id, i)]] 
    if (is.null(value)) NA else value 
  })) 
}

Data<-data.frame(Proj=1:10)
Data$Exclude<-shinyInput(actionLink, nrow(Data), '', label = p(tags$b("X"),align="center",style="color:red; font-size:100%; text-align:center;")
                                , onclick = 'Shiny.onInputChange(\"deleteRows\",  this.id)' )
Data$Proj<-shinyInput2(numericInput, nrow(Data), 'Proj_', Data$Proj,width='40px')

values <- reactiveValues(dfWorking = Data)

hid<-reactive({
  shinyValue('Proj_',nrow(values$dfWorking))
})


output$table <- renderDataTable({
  datatable(values$dfWorking,escape = FALSE,selection='single',rownames=FALSE
            ,options = list(pageLength=nrow(values$dfWorking)
                            ,autoWidth = TRUE 
                            ,preDrawCallback = JS('function() { 
                                                   Shiny.unbindAll(this.api().table().node()); }') 
                            ,drawCallback = JS('function() { 
                                                Shiny.bindAll(this.api().table().node()); } ')
                            #aoColumnDefs = list(list(sWidth="300px", aTargets=c(list(0),list(1))))    # custom column size
                                 )
                 )
                                 }
  ,server=FALSE
  )


observeEvent(input$deleteRows, {
  values$dfWorking <- values$dfWorking[-as.numeric(input$table_cell_clicked$row),]

})
output$text<-renderPrint({hid()})


}

ui <- fluidPage(dataTableOutput("table"),verbatimTextOutput("text"))


shinyApp(ui = ui, server = server)


Thanks!

(I can provide more detail on what I've tried to solve my problem but I thought it may be a simple problem and so I've kept the example as simple as possible for clarity.)

Joe Cheng

unread,
Feb 21, 2017, 12:25:49 PM2/21/17
to Matthew Brown, Shiny - Web Framework for R, Yihui Xie
It doesn't appear to be about deleting, but rather, anything that causes renderDataTable to invalidate. I tested this by putting an actionButton in the UI, and referring to it from within the renderDataTable. Once the button is clicked, the numeric inputs don't work.

I think the problem is that Shiny.unbindAll isn't called right before the table is replaced, so all the old bindings are still active, and the new numericInputs therefore can't be bound because they share the same input IDs. I'm cc-ing Yihui who might have a better idea about how to handle this.

I will also say that this appears to be a bit of an awkward fit for DT anyway, you might consider rhandsontable instead (I actually don't know that much about rhandsontable but it looks like it's intended for what you're doing in this example).

--
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/59719c35-1631-4a74-8333-728ef1e03e85%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthew Brown

unread,
Feb 22, 2017, 11:43:24 AM2/22/17
to Shiny - Web Framework for R
Thanks Joe! I would prefer to user DT, if possible, because I have implemented many other features and am not sure I'd be able to easily transfer them to another data table package. If anyone else has tips or suggestions on how to get DT to work, I'd appreciate it!

 I will test out the rhandsontable package to see if it meets my needs and share the results if I'm able to get it working properly. 
Reply all
Reply to author
Forward
0 new messages