根据一列中的字符串值为 rhandsontable 中的整行着色

Color a whole row in rhandsontable based on a string value in one column

我有一个 rhandsontable,如果最后一列 ("Comments") 文本中的单元格包含字符串 "missed".[=,我希望整行为黄色12=]

下面的代码突出显示具有值 'missed' 的任何单元格,但不突出显示整行。此外,我希望当最后一列中的单元格包含 "missed" 时,该行变成黄色 - 即使在较长的字符串中,而不仅仅是当它只包含 "missed" - 现在写的 -我只是不知道如何匹配 JavaScript.

中的字符串
DF = data.frame(a = 1:2, b = 3:4, Comments = c("missed etc", "missed"))
rhandsontable(DF, width = 550, height = 300) %>%
  hot_cols(renderer = " function (instance, td, row, col, prop, value, cellProperties) { 
                     Handsontable.renderers.TextRenderer.apply(this, arguments); 
                     if(value == 'missed') { 
                     td.style.background = 'yellow' } 
                     }")

非常感谢!

为了澄清,实际上,我正在处理我在 rShiny 中渲染的更大的 table。因此,理想情况下,适用于上面的小数据框的解决方案也应该适用于此。目前它不是(什么都没有显示):

    output$session_table <- renderRHandsontable({
        req(input$select_a_patient)
        patient_nr <- which(patient_names_reactive$names %in% input$select_a_patient)

        row_highlight = which(grepl("missed", 
sessions_reactive$sessions[[patient_nr]]$Comments))-1

        rhandsontable(sessions_reactive$sessions[[patient_nr]],
                      row_highlight = row_highlight,
                      width = 1000, height = 500) %>% 
            hot_rows(fixedRowsTop = 1) %>%
            hot_table(highlightCol = TRUE, highlightRow = TRUE) %>% 
            hot_validate_numeric(cols = c(3, 5), min = 0, max = 500) %>% 
            hot_col(c(1, 3, 5, 6, 8), valign = 'htCenter') %>%
            hot_cols(renderer = " 
            function (instance, td, row, col, prop, value, cellProperties) { 
                     Handsontable.renderers.TextRenderer.apply(this, arguments); 

                     tbl = this.HTMLWidgets.widgets[0]
                     hrows = tbl.params.row_highlight
                     hrows = hrows instanceof Array ? hrows : [hrows]

                     if (hrows.includes(row)) { 
                       td.style.background = 'yellow' } 
                     }") %>% hot_col(c(5, 8), renderer = " 
                        function (instance, td, row, col, prop, value, cellProperties) {
                           Handsontable.renderers.TextRenderer.apply(this, arguments);
                           td.style.fontWeight = 'bold';
                           td.style.color = '#fc0f03';}"
            )

基于使用自定义渲染器的教程:

https://jrowen.github.io/rhandsontable/

您可以执行以下操作(在 js 之外进行字符串匹配)。

DF = data.frame(a = 1:4, b = 3:6, Comments = c("missed etc", "", "missed", ""))

rhandsontable(DF, row_highlight = which(grepl("missed", DF$Comments))-1, width = 550, height = 300) %>%
  hot_cols(renderer = "function (instance, td, row, col, prop, value, cellProperties) { 
                     Handsontable.renderers.TextRenderer.apply(this, arguments); 

                     tbl = this.HTMLWidgets.widgets[0]

                     hrows = tbl.params.row_highlight
                     hrows = hrows instanceof Array ? hrows : [hrows]

                     if (hrows.includes(row)) { 
                       td.style.background = 'yellow' } 
                     }")

编辑:如果用作闪亮的应用程序,这显然需要额外修改。正如 documentation 中所述:

When using this approach in a shiny app or in a document with more than one widget, the widget search logic will need to be more robust.

如果instance.params可用,我们可以尝试以下方法:

library(shiny)
library(rhandsontable)

DF = data.frame(a=1:10, b=3:12, c=c("Dog", "Cat", "Mouse", 5:11), d=3:12, e=1:10, f=1:10, g=1:10, h=2:11, Comments = c("missed etc", rep("", 7), "missed", ""))

ui <- fluidPage(
  mainPanel(
    rHandsontableOutput('table')
  )
)

server = function(input, output, session) {

  output$table <- renderRHandsontable({
    row_highlight = which(grepl("missed", DF$Comments))-1
    col_highlight = c(5,8)-1
    rhandsontable(DF, row_highlight = row_highlight, col_highlight = col_highlight, width = 550, height = 300) %>%
      hot_rows(fixedRowsTop = 1) %>%
      hot_table(highlightCol = TRUE, highlightRow = TRUE) %>% 
      hot_validate_numeric(cols = c(3, 5), min = 0, max = 500) %>% 
      hot_col(c(1, 3, 5, 6, 8), valign = 'htCenter') %>%
      hot_cols(renderer = "
            function (instance, td, row, col, prop, value, cellProperties) {
                     Handsontable.renderers.TextRenderer.apply(this, arguments);

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

                       if (hrows.includes(row)) {
                         td.style.background = 'yellow' 
                       }

                       if (hcols.includes(col)) {
                         td.style.fontWeight = 'bold'
                         td.style.color = '#fc0f03'
                       }
                     }
            }"
      ) 
  })
}

shinyApp(ui, server)

Edit1:将第二个渲染器与第一个渲染器集成在一起,以便列格式不会覆盖行背景颜色。

Edit2:添加关于 col_highlight 的解释。

从一开始我们就有了 col_highlight = c(5,8)-1,它创建了一个向量来存储我们希望具有不同格式(粗体、红色字体)的列。我们减去一个,因为 javascript 中的数组是从零开始的。

rhandsontable 的下一行允许我们传入 col_highlight = col_highlight 以便我们稍后可以通过渲染器函数中的 instance.params.col_highlight 访问这些选定的列。一旦我们访问它们并将它们分配给 hcols,我们确保它是一个数组(如果它还不是)。

语句 if (hcols.includes(col)) 检查 hcols 数组是否包含正在呈现的列 (col)。如果呈现的列为 5,则它包含在向量 (5, 8) 中,并且 td.style 将设置为粗体和红色。

注意,由于hrows只会改变背景色,而hcols只会改变字体的粗体和颜色,所以一个不会覆盖另一个,可以一起使用。