Nando Carmona
Nando Carmona

Reputation: 35

R shiny ColVis and datatable search

I have TableTools and ColVis working together, but, as it was explained in another post (R shiny DataTables ColVis behavior), when clicking the Show/hide columns button, the list mixes up with the values in the table underneath, and I cannot make the list disappear.

In that post is mentioned that shiny atm is not compatible with the current data.table version, and I'd like to know if there's any other solution around. Here's my code:

ui.R

 library(shiny)
 library(shinythemes)
 library(ggplot2)

addResourcePath('datatables','\\Users\\Ser\\Downloads\\DataTables-1.10.7\\DataTables-1.10.7\\media')
addResourcePath('tabletools','\\Users\\Ser\\Downloads\\TableTools-2.2.4\\TableTools-2.2.4')

shinyUI(fluidPage(theme = shinytheme("Journal"),


  tags$head(
tags$style(HTML("
  @import url('//fonts.googleapis.com/css?family=Lobster|Cabin:400,700');
                "))
),

  headerPanel(
h1("List Manager", 
   style = "font-family: 'Lobster', cursive;
   font-weight: 500; line-height: 1.1; 
   color: #ad1d28;")),

sidebarLayout(
    sidebarPanel( 


#File Upload Manager
fileInput('file1', 'Choose file to upload'),

tagList(
  singleton(tags$head(tags$script(src='//cdnjs.cloudflare.com/ajax/libs/datatables/1.10.7/js/jquery.dataTables.min.js',type='text/javascript'))),
  singleton(tags$head(tags$script(src='//cdnjs.cloudflare.com/ajax/libs/datatables-tabletools/2.1.5/js/TableTools.min.js',type='text/javascript'))),
  singleton(tags$head(tags$script(src='//cdnjs.cloudflare.com/ajax/libs/datatables-tabletools/2.1.5/js/ZeroClipboard.min.js',type='text/javascript'))),
  singleton(tags$head(tags$link(href='//cdnjs.cloudflare.com/ajax/libs/datatables-tabletools/2.1.5/css/TableTools.min.css',rel='stylesheet',type='text/css'))),
  singleton(tags$head(tags$script(src='//cdn.datatables.net/colvis/1.1.0/js/dataTables.colVis.min.js',type='text/javascript'))),
  singleton(tags$script(HTML("if (window.innerHeight < 400) alert('Screen too small');")))
)),

mainPanel(
dataTableOutput("mytable"))

  )))

server.R

shinyServer(function(input, output) {
  output$mytable = renderDataTable({

inFile <- input$file1
if (is.null(inFile))
  return(NULL)
read.table(inFile$datapath, header=TRUE, sep='')

  }, options = list(
"dom" = 'TC<"clear">lfrtip',
"colVis" = list(
  "activate"="click",
  "align"="right"),
"oTableTools" = list(
  "sSwfPath" = "//cdnjs.cloudflare.com/ajax/libs/datatables-tabletools/2.1.5/swf/copy_csv_xls.swf",
  "aButtons" = list(
    "copy",
    "print",
    list("sExtends" = "collection",
         "sButtonText" = "Save",
         "aButtons" = c("csv","xls")
    )
  )
)
  )
  )
})

I also have another question: I'd like to search "<" or ">" values in the searchboxes at the bottom of the table, but I can't make it work. I don't know if I have to add anything to the code so it can be done (such as "regex" or similar).

Upvotes: 1

Views: 1637

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30114

You may try the DT package instead of hacking the JS libraries by yourself. Here is a minimal example:

library(shiny)
library(DT)
library(shinythemes)
shinyApp(
  ui = fluidPage(
    theme = shinytheme('journal'),
    fluidRow(column(12, DT::dataTableOutput('foo')))
  ),
  server = function(input, output) {
    output$foo = DT::renderDataTable(
      iris,
      filter = 'bottom',
      extensions = list(ColVis = list(activate= "click", align = "right")),
      options = list(dom = 'C<"clear">lfrtip')
    )
  }
)

See http://rstudio.github.io/DT/extensions.html for more info on DataTables extensions.

Upvotes: 1

Related Questions