英文:
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')))
英文:
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')))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论