【问题标题】:R shiny ColVis and datatable searchR闪亮的ColVis和数据表搜索
【发布时间】:2015-09-22 07:27:39
【问题描述】:

我有 TableTools 和 ColVis 一起工作,但是,正如在另一篇文章 (R shiny DataTables ColVis behavior) 中解释的那样,当单击显示/隐藏列按钮时,列表与下面表格中的值混合在一起,我不能使列表消失。

在那篇文章中提到闪亮的 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")
    )
  )
)
  )
  )
})

我还有一个问题:我想在表格底部的搜索框中搜索“”值,但无法正常工作。我不知道是否必须在代码中添加任何内容才能完成(例如“正则表达式”或类似内容)。

【问题讨论】:

  • 以后请每帖问一个问题。

标签: regex r datatables shiny


【解决方案1】:

您可以尝试 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

【讨论】:

  • 感谢@Yihui,我正在使用 DT 包进行操作,但现在 tabletools 选项不起作用(出现按钮但不复制或保存)并且“过滤器”选项返回此消息:警告在 run(timeoutMs) 中:max 没有非缺失参数;返回 -Inf -。不知道是编辑原始帖子还是打开一个新帖子更好。问候!
  • 我认为您有一个列,其中所有值都是 NA。您可能想要删除该列。对于 TableTools 问题,请再次查看文档:rstudio.github.io/DT/extensions.html
  • 如果我在通过 Shiny 运行应用程序时包含过滤器选项,但在我上传文件之前,我会收到该警告。也许它可能与 read.table 参数(read.table(infile$datapath, header = TRUE, sep = ""))有关,但不知道该怎么做。如果删除过滤器选项,则没有问题或警告,应用程序运行正常。
猜你喜欢
  • 2014-08-29
  • 2014-08-27
  • 1970-01-01
  • 2018-06-26
  • 2017-07-12
  • 2014-05-13
  • 2018-12-02
  • 1970-01-01
  • 2014-09-18
相关资源
最近更新 更多