How to pass variable to targets in the options for renderDataTable (shiny)

The following can disable filter for column 1 and column 3 in the rendered data table:

output$datatbl <- DT::renderDataTable(
dt$df, rownames = FALSE,
filter = 'top',
options = list(autoWidth = TRUE, 
               columnDefs = list(list(targets = c(1,3), searchable = FALSE))))

However, I have different datasets uploaded to my app, so the column to be disabled for search will be different. I need to pass variable to update the value for targets in the options. I tried the following (the expr inside eval(substitute() is to catch the indices of the columns which are found in a reactive value list dt$datecolchoices):

output$datatbl <- DT::renderDataTable(
dt$df, rownames = FALSE,
filter = 'top',
options = list(autoWidth = TRUE, 
               columnDefs = list(list(targets = eval(substitute(which(names(dt$df) %in% dt$datecolchoices))), searchable = FALSE))))

However, it seems the above doesn't pass the value of the var to targets as I expected as the columns are not disabled for filter. Is there anything wrong with my code? Thanks!

1 answer

  • answered 2017-06-17 19:47 Steven Mortimer

    You can determine the column names by position beforehand and pass them into the targets argument like this:

    output$datatbl <- DT::renderDataTable({
    
      disable_search_targets <- which(colnames(iris) %in% c('Petal.Length', 'Species'))
    
      datatable(iris, 
                options = list(autoWidth = TRUE, 
                               columnDefs = list(list(targets = disable_search_targets, 
                                                      searchable  = FALSE))))
    })