Shiny and rhandsontable - условное форматирование ячеек / столбцов на основе суммы столбцов - PullRequest
1 голос
/ 07 ноября 2019

У меня есть блестящее приложение в блестящем приложении. Моя цель - покрасить все ячейки в столбце на основе суммы столбца. Пример: если сумма значений в столбце равна 1, то все ячейки в этом столбце окрашиваются в зеленый цвет.

Ожидаемый результат, отображаемый для пользователя, выглядит следующим образом:

expected displayed result

Кажется возможным сделать это с помощью 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")
    })

Я безуспешно пытался использовать эти сообщения и учебные пособия:

Любая идея приветствуется:)

1 Ответ

1 голос
/ 07 ноября 2019

Мы можем увидеть это решение здесь rhandsontable - Custom Renderer , однако, это решение имеет проблему, когда мы реализуем в блестящем, к счастью, проблема была решена здесь

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)

enter image description here

...