闪亮和 rhandsontable - 基于列总和的条件 cell/column 格式

Shiny and rhandsontable - Conditional cell/column formatting based on column sum

我在闪亮的应用程序中有一个 rhandsontable。 我的目标是根据列的总和为列中的所有单元格着色。 例如:如果列中值的总和为 1,则此列中的所有单元格都显示为绿色。

显示给用户的预期结果是这样的:

使用这样的 JS 格式似乎可以做到这一点:

rhandsontable(myrhandsontable) %>% 
        hot_cols(renderer ="some JS script")

我以前从未做过任何 JS,我很难得到“some JS script”部分内的列的总和。

这是一个最小的可复制示例:

library(shiny)
library(rhandsontable)
library(tidyverse)

# basic user interface (not important here)
ui <- fluidPage(
    rHandsontableOutput(outputId = "ex")
)

# server side calculations
server <- function(input, output) {
    # create a dummy dataset
    ex_data = data.frame(id = letters[1:3],
                         attr1 = c(0.5, 0.4, 0.3),
                         attr2 = c(0.6, 0.3, 0.1))

    # create the rhandsontable object and define conditional formatting
    output$ex = renderRHandsontable({
        rhandsontable(ex_data) # %>% renderer ="JS script for conditional formatting")
    })

我尝试使用这些帖子和教程没有成功:

欢迎任何想法:)

我们可以在这里看到这个解决方案rhandsontable - Custom Renderer, however, this solution has an issue when we implement in shiny, luckily, the issue has been resolved here

library(shiny)
library(tidyverse)
library(rhandsontable)

# basic user interface (not important here)
ui <- fluidPage(
  rHandsontableOutput(outputId = "ex")
)

# server side calculations
server <- function(input, output, session) {
  # create the rhandsontable object and define conditional formatting
  output$ex = renderRHandsontable({
    # create a dummy dataset
    ex_data = data.frame(id = letters[1:3],
                         attr1 = c(0.5, 0.4, 0.3),
                         attr2 = c(0.6, 0.3, 0.1))
    #create index with columns sum is equal to 1
    col_highlight <- unname(which(colSums(ex_data[c(2,3)])==1))

    rhandsontable(ex_data, col_highlight = col_highlight,
                  width = 550, height = 300) %>%
      hot_cols(renderer = "
    function(instance, td, row, col, prop, value, cellProperties) {
      Handsontable.renderers.NumericRenderer.apply(this, arguments);

      if (instance.params) {
            hcols = instance.params.col_highlight;
            hcols = hcols instanceof Array ? hcols : [hcols];
          }

      if (instance.params && hcols.includes(col)) {
        td.style.background = 'lightgreen';
      }
  }")
  })

}
shinyApp(ui,server)