簡體   English   中英

如何根據R Shiny DT中的模式突出顯示一行中相鄰單元格的串,每個單元格包含單個字符

[英]How to highlight bunch of adjacent cells in a row containing single character per cell based on a pattern in R Shiny DT

此查詢的擴展,我前面的查詢在這個論壇。 但是,這一次我必須連續處理一系列字符,每個字符在一個單元格中,如下圖所示。 我想根據如下模式突出顯示或更改某些單元格的背景:

  1. 任何包含A?C等字母的相鄰單元格在哪里? 可以是任何字母或

  2. 單元格中的M

  3. 相鄰單元格中的N和F類似於NF ,如下圖所示。

換句話說,需要將此表轉換為 相鄰列單元格中的字母系列

使用R Shiny 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函數將接受您向其拋出的任何perl正則表達式。 AC表示A,后接任意字母,后接C。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM