如何在R Shiny中显示折叠的DT中的行组计数?

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

How to show the rowGroup count in a collapsed DT in R Shiny?

问题

我在查看这个之前的问题https://stackoverflow.com/questions/59896704/collapse-rowgroup-shiny以及提供的解决方案。

library(shiny)
library(DT)
ui <- fluidPage(# Application title
  titlePanel("Collapse/Expand table"),
  mainPanel(DTOutput("my_table"))
)

callback_js <- JS(
    "table.on('click', 'tr.dtrg-group', function () {",
    "  var rowsCollapse = $(this).nextUntil('.dtrg-group');",
    "  $(rowsCollapse).toggleClass('hidden');",
    "});",
    "table.one('init', () => $('#my_table .dtrg-group').trigger('click'))"
  )

server <- function(input, output) {
  output$my_table <- DT::renderDT({
    datatable(
      mtcars[1:15, 1:5],
      extensions = 'RowGroup',
      options = list(rowGroup = list(dataSrc = 2), pageLength = 20),
      callback = callback_js,
      selection = 'none'
    )
  })
}

我成功改进了JS代码,以便在表格的唤醒视图中进行了折叠。在此示例中,数据根据dataSrc = 2进行折叠,对应于mtcars表格的第2列,即'cyl'列。我需要的是,当表格折叠时,能够看到每个cyl类别的汽车数量。我该如何做到这一点?

英文:

I'm looking at this previous question https://stackoverflow.com/questions/59896704/collapse-rowgroup-shiny and the solution provided.

library(shiny)
library(DT)
ui &lt;- fluidPage(# Application title
  titlePanel(&quot;Collapse/Expand table&quot;),
  mainPanel(DTOutput(&quot;my_table&quot;)))

callback_js &lt;- JS(
    &quot;table.on(&#39;click&#39;, &#39;tr.dtrg-group&#39;, function () {&quot;,
    &quot;  var rowsCollapse = $(this).nextUntil(&#39;.dtrg-group&#39;);&quot;,
    &quot;  $(rowsCollapse).toggleClass(&#39;hidden&#39;);&quot;,
    &quot;});&quot;,
    &quot;table.one(&#39;init&#39;, () =&gt; $(&#39;#my_table .dtrg-group&#39;).trigger(&#39;click&#39;))&quot;
  )

server &lt;- function(input, output) {
  output$my_table &lt;- DT::renderDT({
    datatable(
      mtcars[1:15, 1:5],
      extensions = &#39;RowGroup&#39;,
      options = list(rowGroup = list(dataSrc = 2), pageLength = 20),
      callback = callback_js,
      selection = &#39;none&#39;
    )
  })
}

# Run the application
shinyApp(ui = ui, server = server)

I managed to enhance the JS such that the wakeup view of this table is collapsed. Data in this example is collapsed based off dataSrc = 2 that corresponds to the 2nd column of the mtcards table, that is, the 'cyl' column. What I need: I would need to be able to see the count of cars for each cyl category when the table is collapsed. How can I do that?

答案1

得分: 2

以下是您提供的代码的中文翻译部分:

这里有一个示例,其中每个类别的计数写入折叠的行组,通过编辑相应的 innerHTML 来实现。您可以通过将以下 JS 添加到回调中来实现这一点。

table.on('init', () => $('#my_table .dtrg-group').each(function(i, obj) {
    var grpcount = $(this).nextUntil('.dtrg-group').length.toString();
    this.innerHTML = this.innerHTML.replace('</th>', ', count: ' + grpcount + '</th>');
}));
library(shiny)
library(DT)

ui <- fluidPage(# Application title
    titlePanel("折叠/展开表格"),
    mainPanel(DTOutput("my_table"))
)

callback_js <- JS(
    "table.on('click', 'tr.dtrg-group', function () {",
    "  var rowsCollapse = $(this).nextUntil('.dtrg-group');",
    "  $(rowsCollapse).toggleClass('hidden');",
    "});",
    "table.one('init', () => $('#my_table .dtrg-group').trigger('click'))",
    "table.on('init', () => $('#my_table .dtrg-group').each(function(i, obj) {",
    "    var grpcount = $(this).nextUntil('.dtrg-group').length.toString();",
    "    this.innerHTML = this.innerHTML.replace('</th>', ', count: ' + grpcount + '</th>');",
    "}));"
)

server <- function(input, output) {
    output$my_table <- DT::renderDT({
        datatable(
            mtcars[1:15, 1:5],
            extensions = 'RowGroup',
            options = list(
                rowGroup = list(dataSrc = 2),
                pageLength = 20
            ),
            callback = callback_js,
            selection = 'none'
        )
    })
}

# 运行应用程序
shinyApp(ui = ui, server = server)

请注意,这只是代码的翻译部分,没有其他内容。

英文:

Here is one example where the count of each category is written into the collapsed row groups by editing the corresponding innerHTML. You can get this by adding the following JS to your callback.

table.on(&#39;init&#39;, () =&gt; $(&#39;#my_table .dtrg-group&#39;).each(function(i, obj) {
    var grpcount = $(this).nextUntil(&#39;.dtrg-group&#39;).length.toString();
    this.innerHTML = this.innerHTML.replace(&#39;&lt;/th&gt;&#39;, &#39;, count: &#39; + grpcount + &#39;&lt;/th&gt;&#39;);
}));&quot;

如何在R Shiny中显示折叠的DT中的行组计数?

library(shiny)
library(DT)

ui &lt;- fluidPage(# Application title
    titlePanel(&quot;Collapse/Expand table&quot;),
    mainPanel(DTOutput(&quot;my_table&quot;)))

callback_js &lt;- JS(
    &quot;table.on(&#39;click&#39;, &#39;tr.dtrg-group&#39;, function () {&quot;,
    &quot;  var rowsCollapse = $(this).nextUntil(&#39;.dtrg-group&#39;);&quot;,
    &quot;  $(rowsCollapse).toggleClass(&#39;hidden&#39;);&quot;,
    &quot;});&quot;,
    &quot;table.one(&#39;init&#39;, () =&gt; $(&#39;#my_table .dtrg-group&#39;).trigger(&#39;click&#39;))&quot;,
    &quot;table.on(&#39;init&#39;, () =&gt; $(&#39;#my_table .dtrg-group&#39;).each(function(i, obj) {&quot;,
    &quot;    var grpcount = $(this).nextUntil(&#39;.dtrg-group&#39;).length.toString();&quot;,
    &quot;    this.innerHTML = this.innerHTML.replace(&#39;&lt;/th&gt;&#39;, &#39;, count: &#39; + grpcount + &#39;&lt;/th&gt;&#39;);&quot;,
    &quot;}));&quot;
)

server &lt;- function(input, output) {
    output$my_table &lt;- DT::renderDT({
        datatable(
            mtcars[1:15, 1:5],
            extensions = &#39;RowGroup&#39;,
            options = list(
                rowGroup = list(dataSrc = 2),
                pageLength = 20
            ),
            callback = callback_js,
            selection = &#39;none&#39;
        )
    })
}

# Run the application
shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年8月5日 00:20:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837663.html
匿名

发表评论

匿名网友

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

确定