How do I hide a column from a DT table output?

huangapple go评论50阅读模式
英文:

How do I hide a column from a DT table output?

问题

DT包展示了如何在表格中进行条件格式化的代码示例[example][1]。

library(DT)
library(tidyverse)

df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))

datatable(df) %>% formatStyle(
'V1',
backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)


如何从最终输出中隐藏```V6```列?

[1]: https://rstudio.github.io/DT/010-style.html
英文:

The DT package shows the following code example to conditionally format a table.

library(DT)
library(tidyverse)

df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))

datatable(df) %>% formatStyle(
    'V1', 'V6',
    backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)

How can I hide the V6 column from the final output?

答案1

得分: 0

你可以在 datatable() 函数内使用 "columnDefs" 选项。这将使 V6 列在最终输出中被隐藏。你必须创建一个 "columnDefs" 选项的选项列表(包括 visible=(),这是一个逻辑选项,以及 targets(),表示列索引),以便 datatable() 函数确定列的可见性:

library(DT)
library(tidyverse)
df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), 
sample(0:1, 10, TRUE)))

# 创建 DT 表格
datatable(df
, options = list(columnDefs = list(list(visible=FALSE, 
targets=c(6))))) %>%
  formatStyle('V1','V6', backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow')))

How do I hide a column from a DT table output?

英文:

You can use "columnDefs" option within datatable() function. This will make the V6 column hidden from view in the final output. You must create a list of options for "columnDefs" (visibile=() which is logical option & targets() indicating column index) and therefore for the datatable() function to determine the visibility of the columns:

library(DT)
library(tidyverse)
df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), 
sample(0:1, 10, TRUE)))

# create DT table
datatable(df
, options = list(columnDefs = list(list(visible=FALSE, 
targets=c(6))))) %>% 
  formatStyle('V1','V6', backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow')))

How do I hide a column from a DT table output?

huangapple
  • 本文由 发表于 2023年2月14日 07:00:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定