如何使用DT包隐藏列-columnDefs参数不起作用 [英] How to hide a column using the DT package - columnDefs parameter doesn't work

查看:202
本文介绍了如何使用DT包隐藏列-columnDefs参数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用DT包在数据框中隐藏一列(在下面的示例中为col4)。

I would like to hide a column (col4 in example below) in a dataframe using the DT package.

我已经合并了找到的代码段,此处无济于事-col4仍然显示。这是我的sessionInfo,以及可复制的示例。

I've incorporated the code snippet found here, to no avail - col4 still shows. Here is my sessionInfo, along with my reproducible example.

> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils    
[5] datasets  methods   base     

other attached packages:
[1] DT_0.1.55    shiny_0.13.1

loaded via a namespace (and not attached):
 [1] htmlwidgets_0.6 magrittr_1.5   
 [3] R6_2.1.2        htmltools_0.3.5
 [5] tools_3.2.3     yaml_2.1.13    
 [7] Rcpp_0.12.4     jsonlite_0.9.19
 [9] digest_0.6.9    xtable_1.8-2   
[11] httpuv_1.3.3    mime_0.4 


library(shiny)
library(DT)

ui <- fluidPage(
                dataTableOutput("testtable")
                ) #close fluidpage


server <- function(input, output, session){

  output$testtable <- DT::renderDataTable({  
                                          test <- data.frame(a = runif(10), b = runif(10), c = runif(10), 
                                                             col4 = c('a', 'b', 'b', 'a', 'c', 'b', 'c', 'b', 'b', 'a'))
                                          datatable(test, options=list(columnDefs = list(list(visible=FALSE, targets='col4')))) %>%
                                            formatStyle(columns = 1, valueColumns = 'col4', 
                                                        backgroundColor = styleEqual('a', 'green')
                                                        ) %>% #close formatstyle
                                            formatStyle(columns = 2, valueColumns = 'col4', 
                                                        backgroundColor = styleEqual('b', 'green')
                                                        ) %>% #close formatstyle
                                            formatStyle(columns = 3, valueColumns = 'col4', 
                                                        backgroundColor = styleEqual('c', 'green')
                                                        ) #close formatstyle
                                          }) #close renderDataTable

                                          } # closer server

shinyApp(ui=ui, server=server)


推荐答案

目标应该是列号的数值

datatable(test, options=list(columnDefs = list(list(visible=FALSE, targets=c(4))))) %>%

这篇关于如何使用DT包隐藏列-columnDefs参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆