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

北城以北 提交于 2020-01-23 21:41:54

问题


I have an rhandsontable inside a shiny app. My goal is to color all cells in a column based on the sum of the column. Ex : if the sum of the values in the column is 1, then, all cells in this column are colored in green.

The expected outcome displayed to the user is like this:

It seems possible to do so with a JS formatting like this :

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

I have never done any JS before and I struggle to get the sum of the column inside the "some JS script" part.

Here is a minimal reproductible exemple to work with:

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")
    })

I unsuccessfully tried to use these posts and tutorials:

  • rhandsontable change background of specific row
  • Rhandsontable conditional formatting- How to highlight rows based on specific attribute value?
  • https://jrowen.github.io/rhandsontable/#custom_renderer_using_r
  • https://jrowen.github.io/rhandsontable/#conditional_formatting

Any idea is welcome :)


回答1:


We can see this solution here 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)



来源:https://stackoverflow.com/questions/58746194/shiny-and-rhandsontable-conditional-cell-column-formatting-based-on-column-sum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!