Include javascript in Shiny

Asked

Viewed 70 times

2

I would like to include the event "Restoring column visibility (https://datatables.net/extensions/buttons/examples/column_visibility/restore.html), the code would be the one below:

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

In my code in R, could someone 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)

})
  • It is not clear what your problem is. What you would like to happen and doesn’t happen?

  • Tomás, I would like to include the event "Restoring the visibility of the column".

  • https://datatables.net/extensions/buttons/examples/column_visibility/restore.html

  • For me already appears with your code. Including without including the JS

  • The problem happens if you carry the shiny after the DT. Because he writes about the functions dataTableOutput() and renderDataTable(). Ask the question with a reproducible example helping.

1 answer

3


Solution

Below is a minimum and reproducible example of the solution. I switched your base to mtcars and the 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 carry the shiny after the DT. Because he writes about the functions dataTableOutput() and renderDataTable().

One way around this type of problem is to say explicitly in the function call in which package the R it shall be retrieved using the operator ::.

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

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

  • Obrigado Tomás.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.