根据一列中的字符串值,以rhosonsontable为整行着色 [英] Color a whole row in rhandsontable based on a string value in one column

查看:129
本文介绍了根据一列中的字符串值,以rhosonsontable为整行着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可录音的书,并且如果最后一列(注释")中的文本单元格包含字符串"missed",我希望整个行都为黄色.

I have an rhandsontable and I want the WHOLE ROW to be yellow if the cell in the text in the last column ("Comments") includes string "missed".

下面的代码突出显示具有丢失"值的所有单元格,但不突出显示整个行.此外,我希望当最后一列中的单元格包含丢失"时,即使在更长的字符串中,行也变为黄色,而不仅仅是当它仅包含丢失"时(如现在所写),我会将该行变为黄色不知道如何在JavaScript中匹配字符串.

The code below highlights any cell that has a value 'missed', but not the whole row. Besides, I'd like the row to turn yellow when a cell in the last column CONTAINS "missed" - even inside a longer string, and not just when it contains only "missed" - as it is written now - I just don't know how to match strings in 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中渲染的一个更大的表.因此,理想情况下,适用于上述小数据帧的解决方案也应在这里使用.当前还没有(什么都没有显示):

To clarify, in reality, I am dealing with a larger table that I am rendering in rShiny. So, ideally the solution that works for the little data frame above should also work here. Currently it is not (nothing shows up at all):

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

推荐答案

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

Based on the tutorial using a custom renderer:

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

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

you could do the following (doing the string match outside of the 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' } 
                     }")

编辑:如果用作闪亮的应用,显然需要进行其他修改.如文档中所述:

Edit: This apparently requires additional modification if used as a shiny app. As noted in 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可用,我们可以尝试以下操作:

If instance.params is available, we can try the following:

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 :将第二个渲染器与第一个渲染器集成在一起,以使列格式不会覆盖行背景色.

Edit1: Integrated second renderer with the first one, so that column formatting doesn't overwrite row background color.

Edit2 :添加有关添加的col_highlight的说明.

Edit2: Adding explanation regarding col_highlight added.

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

Starting at the beginning we have col_highlight = c(5,8)-1 which creates a vector storing the columns we wish to have different formatting (bold, red font). We subtract one since arrays in javascript are zero-based.

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

The following line for rhandsontable allows us to pass in col_highlight = col_highlight so that we can later access these selected columns through instance.params.col_highlight in the renderer function later on. Once we access them and assign them to hcols, we make sure it is an array if it wasn't already.

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

The statement if (hcols.includes(col)) checks to see if the hcols array contains the column (col) being rendered. If the column rendered is 5, this is contained in the vector (5, 8), and the td.style would be set to bold and red.

请注意,由于hrows仅会更改背景颜色,而hcols仅会更改字体的粗体和颜色,因此一个不会覆盖另一个,并且可以一起使用.

Note that since hrows would only change background color, and hcols would only change font bold and color, one does not overwrite the other, and can be used together.

这篇关于根据一列中的字符串值,以rhosonsontable为整行着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆