I have created a datatable which has selectInput widgets in one of the columns. Another column of the datatable should take inputs given in the first column, and use them to look up a number from my data source. The inputs are binding correctly in Shiny, by using preDrawCallback and drawCallback functions, but lookup values are not updating when the inputs change. Strangely, they do update when I do the lookup in a separate data table. A reproducible example is here:
library(shiny)
library(DT)
data <- data.frame(c(1:7),c(21:27))
shinyApp(
server = shinyServer(function(input, output) {
output$table <- DT::renderDataTable({
Rows <- c(1:7)
temp <- data.frame(Rows)
temp[,"Item"] <- ""
temp[,"Value"] <- ""
temp$Rows <- NULL
sapply(1:7, FUN = function(i) {
temp$Item[i] <<- as.character(selectInput(paste("Item.1.1",i, sep = "."), "",
choices = setNames(c(1:7),c(1:7)),
selected = 1,
multiple = FALSE))
})
sapply(1:7, FUN = function(i) {
temp$Value[i] <<- data[eval(parse(text = paste("input$Item.1.1",i, sep = "."))),2]
})
datatable(temp, escape = FALSE, rownames = FALSE,
options = list(sort = FALSE, paging = FALSE, searching = FALSE, dom = 't',
columnDefs = list(list(className = 'dt-center', targets = 0:1)),
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
))
}, server = FALSE)
}),
ui = fluidPage(
dataTableOutput("table")
)
)
That gives the error "Error in temp$Value[i] <<- data[eval(parse(text = paste("input$Item.1.1", :
replacement has length zero".
My best guess is that unbindAll is clearing out all of my input$Item values before the Value column is created, but bindAll isn't taking affect until after trying to create Value.
Regardless of whether my guess is right, I much appreciate any help that can be offered.
P.S. I have tried creating a second datatable, which is built from the Input$Item's, and it seems to update fine---provided I comment out the code that builds my Value column. Things are *much* simplified for my user when I can have them all in the same datatable, so I really don't want to have to do this if it can be avoided. I'll provide this half-working example in another post following shortly.