Dynamic datatable (DT) with numericInput

148 views
Skip to first unread message

jro

unread,
Aug 11, 2015, 6:11:49 PM8/11/15
to Shiny - Web Framework for R

I added this question to the end of the thread here:

but maybe it was more appropriate to create a new thread since that was already answered.  

That question addressed a problem I was struggling with around re-binding when embedding numeric inputs into a datatable.  However, I still have an issue that if I dynamically regenerate the table based on a parameter changing (e.g., a number of rows slider) all of the bindings are lost when the table is redrawn.  I also tried to unbind/bindall from the js console and was still unsuccessful. I've added the "slider_num_rows" to the example code from Yihui Xie in that question to illustrate the problem in a simplified way.

(separately, the data is lost too, but I'm hoping to feed the 'value' parameter of numericInput with the current input[[...]] value once I get the binding portion working). 

I'm running with 

> packageVersion('shiny')
[1] ‘0.12.1.9001’
> packageVersion('DT')
[1] ‘0.1.30’

any ideas?

Here is the code:

# need the development version of shiny due to https://github.com/rstudio/shiny/pull/857
if (packageVersion('shiny') < '0.12.0.9003')
  devtools
::install_github('rstudio/shiny')
library
(shiny)
library
(DT)


shinyApp
(
  ui
= fluidPage(
   
   
# add slider to control number of rows shown in table
    sliderInput
("slider_num_rows", "Num Rows", min = 0, max = 100, value = 10),
   
      DT
::dataTableOutput('x1'),
    verbatimTextOutput
('x2')
 
),
 
  server
= function(input, output) {
   
# create a character vector of shiny inputs
    shinyInput
= function(FUN, len, id, ...) {
      inputs
= character(len)
     
for (i in seq_len(len)) {
        inputs
[i] = as.character(FUN(paste0(id, i), label = NULL, ...))
     
}
      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
     
}))
   
}
   
   
# a sample data frame
    res
= data.frame(
      v1
= shinyInput(numericInput, 100, 'v1_', value = 0),
      v2
= shinyInput(checkboxInput, 100, 'v2_', value = TRUE),
      v3
= rnorm(100),
      v4
= sample(LETTERS, 100, TRUE),
      stringsAsFactors
= FALSE
   
)
   
   
# render the table containing shiny inputs
    output$x1
= DT::renderDataTable(
     
     
# use slider to control number of rows shown in table
      res
[1:input$slider_num_rows,], server = FALSE, escape = FALSE, options = list(
        preDrawCallback
= JS('function() {
                             Shiny.unbindAll(this.api().table().node()); }'
),
        drawCallback
= JS('function() {
                          Shiny.bindAll(this.api().table().node()); } '
)
     
)
   
)
   
# print the values of inputs
    output$x2
= renderPrint({
      data
.frame(v1 = shinyValue('v1_', 100), v2 = shinyValue('v2_', 100))
   
})
 
}
)




jro

unread,
Aug 13, 2015, 10:15:48 AM8/13/15
to Shiny - Web Framework for R
FYI, In order to see the issue:

- type a number into a v1 numeric input.  
- verify that the number is shown on the verbatim text output table below
- change the number of rows using the slider, afterwards the table is redrawn
- type a number again into a v1 numeric input
- the verbatim text output table is no longer updated / binded as it should be

Reply all
Reply to author
Forward
0 new messages