【问题标题】:Shiny and rhandsontable - Conditional cell/column formatting based on column sumShiny and rhandsontable - 基于列总和的条件单元格/列格式
【发布时间】:2020-03-03 21:13:17
【问题描述】:

我在一个闪亮的应用程序中有一个 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")
    })

我尝试使用这些帖子和教程失败了:

欢迎任何想法:)

【问题讨论】:

    标签: javascript r shiny rhandsontable


    【解决方案1】:

    我们可以在这里看到这个解决方案rhandsontable - Custom Renderer,但是,当我们在shiny中实现这个解决方案时出现了问题,幸运的是,问题已经解决了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)
    

    【讨论】:

      猜你喜欢
      • 2020-12-02
      • 2023-03-26
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      相关资源
      最近更新 更多