I'm trying to make a DT data table with dropdown menus available in R Shiny, similar to this post: How to create a dropdown list in a Shiny table using datatable when editing the table?. I believe I have done everything correct, but the table is not working properly in Shiny. My code is as follows, with the extra js and extra css file in the path, as indicated.
library(DT)
library(shiny)
callback = JS(
"function onUpdate(updatedCell, updatedRow, oldValue){}",
"table.MakeCellsEditable({",
" onUpdate: onUpdate,",
" inputCss: 'my-input-class',",
" confirmationButton: {",
" confirmCss: 'my-confirm-class',",
" cancelCss: 'my-cancel-class'",
" },",
" inputTypes: [",
" {",
" column: 0,",
" type: 'list',",
" options: [",
" {value: 'Keep data', display: 'Keep data'},",
" {value: 'Pass', display: 'Pass'},",
" {value: 'Delete', display: 'Delete'}",
" ]",
" }",
" ]",
"});")
dat <- data.frame(
Action = c("Keep data", "Keep data", "Keep data"),
X = c(1, 2, 3),
Y = c("a", "b", "c")
)
ui <- fluidPage(
DTOutput("table1")
)
server <- function(input, output, session){
## the datatable
dtable <- datatable(
dat, callback = callback, rownames = FALSE,
options = list(
columnDefs = list(
list(targets = "_all", className = "dt-center")
)
)
)
path <- "C:\\Users\\ruben\\Desktop\\Test_QC_Program\\extra_js_css"
dep <- htmltools::htmlDependency(
"CellEdit", "1.0.19", src = path,
script = "dataTables.editCell.js", stylesheet = "dataTables.cellEdit.css")
dtable$dependencies <- c(dtable$dependencies, list(dep))
output$table1 <- renderDT(
{
dtable
},
editable = T, filter = "top", selection = list(mode = 'single', target = 'row+column'))
}
shinyApp(ui,server)
There are several things going wrong: 1) the list of possible selections only appears for the first column (it appears for all 3 columns in the above post), 2) the selection does not stay as the chosen one after choosing "confirm", and 3) I am getting the following warning:
Warning in processWidget(instance) :
renderDataTable ignores ... arguments when expr yields a datatable object; see ?renderDataTable
My guess is that I am not doing something right when trying to render the table in Shiny, but I honestly don't know. I don't know if it's ok for me to copy someone else's code, so I have not copied the dataTables.editCell.js and dataTables.cellEdit.css code to this post. They are available in the above post. Any help would be great. Thank you.