Reputation: 10431
I am trying to change the options of a renderDataTable
call in Shiny to depend on the value of an input variable, in this case a checkbox.
The first checkbox successfully alters the content of the table, adding one more column if checked. The second checkbox does not alter the options of the table. See code below, I am loading an updated version of datatables and other extensions, but that does not seem to have any effect here. The third checkbox also does not alter the formatting of the entries in the table, which is what I ultimately would like to do.
Any ideas?
# server.R
library("ggplot2")
shinyServer(function(input, output, session) {
bold = reactive ({
bold = ''
bold = paste0(bold,'function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {')
bold = ifelse(input$checkbox3,paste0(bold),paste0(bold,'if (parseFloat(aData[0]) >= 0.1) { $("td:eq(0)", nRow).css("font-weight", "bold"); }'))
bold = paste0(bold,'}')
return(bold)
})
output$mytable = renderDataTable({
diamonds[,1:ifelse(input$checkbox1,6,5)]
}, options = list(fnRowCallback = I(bold()),aaSorting=list(list(2, ifelse(input$checkbox2,"asc","desc"))))
)
}
)
# ui.R
shinyUI({
pageWithSidebar(
h1('Diamonds DataTable with TableTools'),
tagList(
singleton(tags$head(tags$script(src='jquery.dataTables.min.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='TableTools.min.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='dataTables.colReorder.min.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='colvis.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='ZeroClipboard.min.js',type='text/javascript'))),
singleton(tags$head(tags$link(href='TableTools.min.css',rel='stylesheet',type='text/css'))),
singleton(tags$head(tags$link(href='ColVis.css',rel='stylesheet',type='text/css')))
, tags$head(
tags$style(HTML("
.cvclear{
text-align:right}")
)
)
),
tabPanel("foo",
checkboxInput("checkbox1", "add one more column", FALSE),
checkboxInput("checkbox2", "sort [desc] or [asc]", FALSE),
checkboxInput("checkbox3", "no bold", FALSE),
dataTableOutput("mytable")
)
)
})
Upvotes: 4
Views: 1692
Reputation: 30425
The option is declared as:
"aaSorting": [[2,'asc']]
which in R is
options = list(aaSorting = list(list(2, 'asc')))
so you need to swap your c
for a list
. However the logic is wrong here.
If sorting is enabled, then DataTables will perform a first pass sort on initialisation. You can define which column(s) the sort is performed upon, and the sorting direction, with this variable. The aaSorting array should contain an array for each column to be sorted initially containing the column's index and a direction string ('asc' or 'desc').
So this option will only work on the initialisation of the table. I dont think Shiny reinitialises the table once it has been formed so this wont work as a reactive only working the first time the table is initialised. Your more complex example may work however and it may just be the list(list(...))
that is the problem.
Upvotes: 2