gpt4 book ai didi

r - 根据筛选值突出显示单元格内容中的部分字符串

转载 作者:行者123 更新时间:2023-12-05 02:19:58 24 4
gpt4 key购买 nike

作为 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

最佳答案

这有点冗长,但 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

关于r - 根据筛选值突出显示单元格内容中的部分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40621174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com