Javascript 如何根据R/DT中的模式高亮显示一行中的一组相邻单元格,每个单元格包含一个字符

Javascript 如何根据R/DT中的模式高亮显示一行中的一组相邻单元格,每个单元格包含一个字符,javascript,r,shiny,dt,Javascript,R,Shiny,Dt,此查询是我在本论坛上的早期查询的扩展。但是,这一次我必须处理一行中的字符序列,每个字符位于一个单元格中,如下图所示。我想根据以下模式突出显示或更改某些单元格的背景: 任何包含字母的相邻单元格,如A?C,其中?可以是任何字母或 在一个单元格或 如下图所示,相邻细胞中的N和F,如NF 换句话说,需要将此表转换为 这将使用R/DT中的rowCallback函数执行。 我在这里推销我的软件包,所以这可能对您不合适,因为它只使用HTML表而不是DT,并且在R端工作,而不是使用javascript fin

此查询是我在本论坛上的早期查询的扩展。但是,这一次我必须处理一行中的字符序列,每个字符位于一个单元格中,如下图所示。我想根据以下模式突出显示或更改某些单元格的背景:

  • 任何包含字母的相邻单元格,如
    A?C
    ,其中
    可以是任何字母或

  • 在一个单元格或

  • 如下图所示,相邻细胞中的N和F,如
    NF

  • 换句话说,需要将此表转换为

    这将使用R/DT中的rowCallback函数执行。

    我在这里推销我的软件包,所以这可能对您不合适,因为它只使用HTML表而不是DT,并且在R端工作,而不是使用javascript

    find_pattern <- function (pat, mat) {
      # collapse the row into a single string:
      strings <- apply(mat, 1, paste0, collapse = '')
      # find the patterns you want:
      found   <- gregexpr(pat, strings, perl = TRUE)
      # the rest is just housekeeping:
      pos <- matrix(FALSE, nrow(mat), ncol(mat))
      lapply(seq_along(found), function (x) {
        matches <- found[[x]]
        lens <- attr(matches, 'match.length')
        if (all(matches == -1)) return()
        for (p in seq_along(matches)) {
          start <- matches[p]
          len <- lens[p]
          end <- start + len - 1
          pos[x, start:end] <<- TRUE 
        }
      })
      which(pos, arr.ind = TRUE)
    }
    
    library(huxtable)
    mydata <- matrix(sample(c('A', 'B', 'C', 'M', 'N', 'F'), 750, replace=TRUE), 3, 250)
    colnames(mydata) <- paste0('X', 1:250)
    myhux <- as_hux(mydata, add_colnames = TRUE)
    myhux <- set_all_borders(myhux, 1)
    background_color(myhux)[1,] <- 'grey'
    background_color(myhux)[myhux == 'M'] <- 'green'
    background_color(myhux)[find_pattern('A.C', myhux)] <- 'yellow'
    background_color(myhux)[find_pattern('NF', myhux)] <- 'blue'
    myhux
    

    find_pattern我在这里推销我的软件包,所以这可能不适合您,因为它只使用HTML表而不是DT,并且在R端工作,而不是javascript

    find_pattern <- function (pat, mat) {
      # collapse the row into a single string:
      strings <- apply(mat, 1, paste0, collapse = '')
      # find the patterns you want:
      found   <- gregexpr(pat, strings, perl = TRUE)
      # the rest is just housekeeping:
      pos <- matrix(FALSE, nrow(mat), ncol(mat))
      lapply(seq_along(found), function (x) {
        matches <- found[[x]]
        lens <- attr(matches, 'match.length')
        if (all(matches == -1)) return()
        for (p in seq_along(matches)) {
          start <- matches[p]
          len <- lens[p]
          end <- start + len - 1
          pos[x, start:end] <<- TRUE 
        }
      })
      which(pos, arr.ind = TRUE)
    }
    
    library(huxtable)
    mydata <- matrix(sample(c('A', 'B', 'C', 'M', 'N', 'F'), 750, replace=TRUE), 3, 250)
    colnames(mydata) <- paste0('X', 1:250)
    myhux <- as_hux(mydata, add_colnames = TRUE)
    myhux <- set_all_borders(myhux, 1)
    background_color(myhux)[1,] <- 'grey'
    background_color(myhux)[myhux == 'M'] <- 'green'
    background_color(myhux)[find_pattern('A.C', myhux)] <- 'yellow'
    background_color(myhux)[find_pattern('NF', myhux)] <- 'blue'
    myhux
    

    find_pattern我非常感谢您的努力。然而,我想突出显示字母行中的一个模式。突出显示特定的单个字母是直截了当的。但是,寻找一种模式并突出显示一组与该模式匹配的单元格是一项挑战。我几乎可以在ExcelVBA中完成这项工作。我正试图在R.DT中实现同样的功能。我将编辑我的答案以更精确地执行您需要的操作-它仍然不是数据表答案,但它可能会给您一个提示。它不必是数据表答案。只要我能够生成一个表格,突出显示一行单元格中的特定模式,我就可以了。上面的答案看起来很有希望。我会尽力实现这一点,然后再联系你。非常努力!我非常感谢你的努力。然而,我想突出显示字母行中的一个模式。突出显示特定的单个字母是直截了当的。但是,寻找一种模式并突出显示一组与该模式匹配的单元格是一项挑战。我几乎可以在ExcelVBA中完成这项工作。我正试图在R.DT中实现同样的功能。我将编辑我的答案以更精确地执行您需要的操作-它仍然不是数据表答案,但它可能会给您一个提示。它不必是数据表答案。只要我能够生成一个表格,突出显示一行单元格中的特定模式,我就可以了。上面的答案看起来很有希望。我会尽力实现这一点,然后再联系你。非常努力!