根据过滤器值突出显示单元格内容中字符串的一部分

时间:2016-11-15 22:53:30

标签: r shiny dt

作为http://rstudio.github.io/DT/010-style.html中提供的示例的下一步,我希望根据过滤器值突出显示单元格中字符串的一部分,如下图所示。我试图以表格形式突出生物序列数据中的特定基序。以下是Excel VBA代码和代表性图像。是否有可能在R中实现这一目标?

Sub SequencePartColourMacro()
Dim Col, Row, FirstRow, LastRow As Integer, Col As Long

Col = 6
FirstRow = 2
LastRow = ThisWorkbook.Sheets("Sequences").Cells(Rows.Count, "F").End(xlUp).Row

Test1 = "CC"
Test2 = "TT"
Test3 = "GG"

For Row = FirstRow To LastRow
   Sequence = ThisWorkbook.Sheets("Sequences").Cells(Row, Col).Value
   For x = 1 To Len(Sequence)
     SubSequence1 = Mid(Sequence, x, 2)
     If SubSequence1 = Test1 Then
       ThisWorkbook.Sheets("Sequences").Cells(Row, Col).Characters(x, 2).Font.Color = RGB(0, 0, 255)
       ThisWorkbook.Sheets("Sequences").Cells(Row, Col).Characters(x, 2).Font.Bold = True
     End If
    If SubSequence1 = Test2 Then
       ThisWorkbook.Sheets("Sequences").Cells(Row, Col).Characters(x, 2).Font.Color = RGB(0, 102, 0)
       ThisWorkbook.Sheets("Sequences").Cells(Row, Col).Characters(x, 2).Font.Bold = True
    End If
    If SubSequence1 = Test3 Then
       ThisWorkbook.Sheets("Sequences").Cells(Row, Col).Characters(x, 2).Font.Color = RGB(255, 153, 0)
       ThisWorkbook.Sheets("Sequences").Cells(Row, Col).Characters(x, 2).Font.Bold = True
    End If
 Next x
 Next Row
 End Sub

Representative image showing highlighted characters

2 个答案:

答案 0 :(得分:2)

这有点冗长,但VBA也是如此:

library(DT)
set.seed(1986)
x <- vector()
# create fake dna or rna sequence (it's been a while since I took bio)
for (i in 1:10) {
  x[i] <- paste0(sample(c("A","G","T","C"),10,replace=TRUE), collapse="")
}

dim(x) <- c(5,2)

datatable(x, options = list(rowCallback=JS(
  "function(row,data) {
    data[0]=data[0].replace('CC','<span style=\"color:red\">CC</span>');
    data[0]=data[0].replace('TT','<span style=\"color:blue\">TT</span>');
    data[0]=data[0].replace('GG','<span style=\"color:green\">GG</span>');
    data[1]=data[1].replace('CC','<span style=\"color:red\">CC</span>');
    data[1]=data[1].replace('TT','<span style=\"color:blue\">TT</span>');
    data[1]=data[1].replace('GG','<span style=\"color:green\">GG</span>');
    $('td:eq(0)', row).html(data[0]);
    $('td:eq(1)', row).html(data[1]);
  }"
  ), dom = 't'))

enter image description here

答案 1 :(得分:1)

@ Carl的代码略有调整,导致识别并突出显示所选字符串的所有匹配项。对我这样的人有用。

library(DT)
set.seed(1986)
x <- vector()
 create fake dna or rna sequence (it's been a while since I took bio)
for (i in 1:10) {
 x[i] <- paste0(sample(c("A","G","T","C"),25,replace=TRUE), collapse="")
}
dim(x) <- c(5,2)

datatable(x, options = list(rowCallback=JS(
  "function(row,data) {
  data[0] = data[0]  .replace(/GA/g,'<span style=\"color:red\">GA</span>');
  data[0] = data[0]  .replace(/TT/g,'<span style=\"color:blue\">TT</span>');
  data[0] = data[0]  .replace(/TC/g,'<span style=\"color:green\">TC</span>');
  data[1] = data[1]  .replace(/GA/g,'<span style=\"color:red\">GA</span>');
  data[1] = data[1]  .replace(/TT/g,'<span style=\"color:blue\">TT</span>');
  data[1] = data[1]  .replace(/TC/g,'<span style=\"color:green\">TC</span>');
  $('td:eq(0)', row).html(data[0]  );
  $('td:eq(1)', row).html(data[1]  );
  }"
  ), dom = 't'))