R 闪亮的 ColVis 和数据表搜索

R shiny ColVis and datatable search

我让 TableTools 和 ColVis 一起工作,但是,正如另一个 post (R shiny DataTables ColVis behavior) 中所解释的那样,当单击 Show/hide 列按钮时,列表与table 下面的值,我不能让列表消失。

其中提到 post shiny atm 与当前的 data.table 版本不兼容,我想知道是否有其他解决方案。这是我的代码:

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")
    )
  )
)
  )
  )
})

我还有一个问题:我想在 table 底部的搜索框中搜索“<”或“>”值,但无法实现。我不知道是否必须在代码中添加任何内容才能完成(例如 "regex" 或类似的)。

您可以尝试 DT 包而不是自己破解 JS 库。这是一个最小的例子:

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')
    )
  }
)

有关 DataTables 扩展的详细信息,请参阅 http://rstudio.github.io/DT/extensions.html