Include javascript in Shiny

2

I would like to include the event "Restore column visibility ( link ), the code would be this below:

$(document).ready(function() {
$('#example').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        {
            extend: 'colvis',
            postfixButtons: [ 'colvisRestore' ]
        }
    ],
    columnDefs: [
        {
            targets: -1,
            visible: false
        }
    ]
} );

In my code in R, could anyone tell me how?

output$dtDisp4 <- renderDataTable({

datatable(baseFullGI, extensions = 'Buttons', options = list(
  dom = 'Bfrtip',
  buttons = list('excel','colvis'),
  searchHighlight = TRUE,
  columnDefs = list(list(className = 'dt-center', targets = 0:11)),
  scrollX = TRUE,
  pageLength = 50
), filter = 'top',
class = 'cell-border stripe',
rownames = FALSE)

})
    
asked by anonymous 01.06.2017 / 20:04

1 answer

3

Solution

Below is a minimal and reproducible example of the solution. I changed your base by mtcars and option targets to 1.

library(DT)
library(shiny)

shinyApp(ui = fluidPage(DT::dataTableOutput("dtDisp4")),
         server = function(input, output) {
           output$dtDisp4 <- DT::renderDataTable({
             DT::datatable(mtcars, extensions = 'Buttons', options = list(
               dom = 'Bfrtip',
               buttons = list('excel','colvis'),
               searchHighlight = TRUE,
               columnDefs = list(list(className = 'dt-center', targets = 1)),
               scrollX = TRUE,
               pageLength = 50
             ), filter = 'top',
             class = 'cell-border stripe',
             rownames = FALSE)
           })
         }
)

Why does this happen?

The problem happens if you load shiny after DT . Because it writes the dataTableOutput() and renderDataTable() functions.

One way to get around this kind of problem is to say explicitly in the function call in which package R should fetch it using the :: operator.

Note that this is only necessary because both packages have functions with the same names and the search engine by R function goes first in that package that was last loaded. This can be clearly observed with the example below. With the environment clean (restart R ), run:

antigo <- search()
library(DT)
novo <- search()
library(shiny)
mais_novo <- search()

No wonder there's a warning when shiny is loaded

  

Attaching package: 'shiny'

     

The following objects are masked from 'package: DT':

     

dataTableOutput, renderDataTable

    
01.06.2017 / 23:57