根据一列中的字符串值,以rhosonsontable为整行着色

时间:2019-09-27 23:02:23

标签: r rhandsontable

我有一个可录音机,如果最后一列(“注释”)中的文本单元格包含字符串“ 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中渲染的一个更大的表。因此,理想情况下,适用于上述小数据帧的解决方案也应在这里使用。目前还没有(什么都没有显示):

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

1 个答案:

答案 0 :(得分:1)

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

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中所述:

  

在闪亮的应用程序或包含更多内容的文档中使用此方法时   比一个小部件而言,小部件搜索逻辑将需要更强大。

如果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仅会更改字体的粗体和颜色,因此一个不会覆盖另一个,并且可以一起使用。

table with select yellow background row and red and bold columns