2017-03-20 20 views
0

此查詢是此論壇中我早期的query的擴展。但是,這次我必須處理單元格中每個字符連續的字符序列,如下圖所示。我想基於像圖案突出或改變某些細胞的背景:如何根據R中的模式突出顯示每行中包含單個字符的相鄰單元格中的相鄰單元格Shiny DT

  1. 含有像A?C字母的任意相鄰的單元格,其中?可以是任何字母或

  2. 只是M在細胞或

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

換句話說,需要該錶轉換成 Series of letters in adjacent column cells

此使用rowCallback功能中的R閃亮DT。 enter image description here

回答

3

我拉皮條我在這裏的包,所以這可能不是你合適,因爲它只是使用了HTML表格不是DT,並適用於未在JavaScript R側。

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 

導致:

enter image description here

find_pattern功能會接受你扔在任何Perl的正則表達式。 A.C表示A,後面跟着任何字母,後面跟着C。

+0

我非常感謝你的努力。但是,我正在着重突出一行字母中的一種模式。突出顯示特定的單個字母非常簡單。但是尋找一種模式並突出顯示一組匹配模式的單元格具有挑戰性。我幾乎可以在Excel VBA中完成這項工作。我正試圖在R Shiny DT中實現相同的功能。 – RanonKahn

+0

我會編輯我的答案,以更準確地完成您的需求 - 它仍不會成爲數據表答案,但它可能會給您一個提示。 – dash2

+0

它不一定是一個數據表答案。只要我能夠在一行單元格中生成突出顯示特定模式的表格,我就很好。 – RanonKahn

相關問題