[英]scroll to row based on user event, R/DT/Shiny, without using paging
[英]How to highlight bunch of adjacent cells in a row containing single character per cell based on a pattern in R Shiny DT
此查詢的擴展,我前面的查詢在這個論壇。 但是,這一次我必須連續處理一系列字符,每個字符在一個單元格中,如下圖所示。 我想根據如下模式突出顯示或更改某些單元格的背景:
任何包含A?C
等字母的相鄰單元格在哪里?
可以是任何字母或
單元格中的M
或
相鄰單元格中的N和F類似於NF
,如下圖所示。
我在這里整理我的包,所以這可能對您不適合,因為它僅使用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.