在R中指定DataTables中隐藏列的顺序。

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

Specify the order in which columns are hidden in DataTables in R

问题

在使用DataTables库创建交互式表格时,当屏幕宽度变得太窄以容纳所有列或列宽度时,列会被隐藏在绿色的+符号下面。随着屏幕宽度减小,右侧的列总是首先被隐藏。

是否可以指定列在屏幕宽度减小时隐藏的顺序?举个例子,假设我希望我的“description”列(每行表格都有一段短文本段落)显示在表格的右侧,并在屏幕宽度减小时保持显示,左侧的列在它之前被隐藏。

library(DT)

df <- data.frame(column_1 = 1:10, 
                 column_2 = rep(1,10), 
                 column_3 = rep("some text"),
                 column_4 = seq(2,29,3),
                 column_5 = 5:14,
                 description = rep("Here is some very long and wordy text. I would like this text to remain hidden under the green + sign that appears when the screen becomes narrow. In real life, I have many more columns than this, and want as many of them to display as possible, but this column to remain hidden even with a very wide screen.", 10))

datatable(df,
          extensions = "Responsive")

注意:我在flexdashboard中使用R DT库,但我认为这个问题对于DataTables的其他用法(例如在JavaScript中)也是相关的。

请参考以下两个相关问题:

英文:

Context

When making an interactive table using the DataTables library, columns are hidden under a green + sign when the screen width becomes too narrow for the number and width of columns. As the screen width decreases, the right hand column is always the next to be hidden.

Question

Is it possible to specify the order in which columns are hidden as the screen width decreases? As an example, say I would like my ‘description’ column (which has a short paragraph of text for each row of the table) to display at the right of my table, and remain showing as the screen width decreases, with columns to its left being hidden before it.

library(DT)

df &lt;- data.frame(column_1 = 1:10, 
             column_2 = rep(1,10), 
             column_3 = rep(&quot;some text&quot;),
             column_4 = seq(2,29,3),
             column_5 = 5:14,
             description = rep(&quot;Here is some very long and wordy text. I would like this text to remain hidden under the green + sign that appears when the screen becomes narrow. In real life, I have many more columns than this, and want as many of them to display as possible, but this column to remain hidden even with a very wide screen.&quot;, 10))

datatable(df,
      extensions = &quot;Responsive&quot;)

Note: I am using the R DT library in flexdashboard, but I expect this question is relevant for other uses of DataTables (e.g. in javascript).

答案1

得分: 0

尝试这个方法,为你的列分配优先级,以供 Responsive 扩展使用:

library(DT)

df <- data.frame(column_1 = 1:10, 
                 column_2 = rep(1, 10), 
                 column_3 = rep("一些文本"),
                 column_4 = seq(2, 29, 3),
                 column_5 = 5:14,
                 description = rep("这里是一些非常长和冗长的文本。我希望这段文本在屏幕变窄时保持隐藏,出现绿色的+符号。在实际生活中,我有比这更多的列,并希望尽可能多地显示它们,但即使在非常宽的屏幕上,这一列也保持隐藏。", 10))

datatable(df,
          extensions = "Responsive",
          options = list(columnDefs = list(
            list(responsivePriority = 10002, targets = 6),
            list(responsivePriority = 10001, targets = 3)
          )))

这将采用纯 DataTables 在 JavaScript 中使用的语法,并将其转换为 R 的语法。

例如:

list(responsivePriority = 10002, targets = 6)

这一行确保列索引 6 具有 Responsive 扩展的最高优先级,这意味着这将是第一个隐藏的列。列索引 6 是包含长文本的列。

第二高的优先级是列索引 3。

其余的列都没有设置优先级,所以我认为它们会根据需要从右到左隐藏。

请记住以下关键点:

  • 列是从零开始索引的。
  • 因为你使用了 Responsive 扩展,所以在表的开头添加了一列(包含显示/隐藏图标),这是列索引 0。

示例截图:

在R中指定DataTables中隐藏列的顺序。

英文:

Try this, which assigns priorities to your columns, for the Responsive extension to use:

library(DT)

df &lt;- data.frame(column_1 = 1:10, 
                 column_2 = rep(1,10), 
                 column_3 = rep(&quot;some text&quot;),
                 column_4 = seq(2,29,3),
                 column_5 = 5:14,
                 description = rep(&quot;Here is some very long and wordy text. I would like this text to remain hidden under the green + sign that appears when the screen becomes narrow. In real life, I have many more columns than this, and want as many of them to display as possible, but this column to remain hidden even with a very wide screen.&quot;, 10))

datatable(df,
          extensions = &quot;Responsive&quot;,
          options = list(columnDefs = list(
            list(responsivePriority = 10002, targets = 6),
            list(responsivePriority = 10001, targets = 3)
          )))

This takes the syntax used by pure DataTables in JavaScript and transforms it to R's syntax.

So, for example:

list(responsivePriority = 10002, targets = 6)

This line ensures that column index 6 has the highest priority for the Responsive extension - meaning that this will be the first column to be hidden. Column index 6 is where you have your long text.

The second highest priority is column index 3.

The remaining columns are all equally unprioritized - so I believe they are hidden from right-to-left as needed.

Remember the following key points:

  • Columns are zero-indexed.
  • Because you are using the Responsive extension, there is one extra column added at the start of your table (to contain the show/hide icon) - and this is column index 0.

Example screenshot:

在R中指定DataTables中隐藏列的顺序。

huangapple
  • 本文由 发表于 2023年6月9日 06:22:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436071.html
匿名

发表评论

匿名网友

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

确定