簡體   English   中英

在R中創建帶有2個向量的腳本

[英]Create a script with 2 vectors in R

我正在使用來自complexheatmap包的complexheatmap

在腳本中,我需要創建一個變量ha_column並將其合並到腳本中。

ha_column = HeatmapAnnotation (df = data.frame(type1=c(rep("name1",5), rep("name2",5),rep("name3",5), col = list(type1=c("name1" =  "#DCDCDC", "name2" = "#DC928B", "name2"="#BA72D3")))))

我有2個向量:

vectors1=c("name1","name2","name3)
vectors2=c("#DCDCDC","#DC928B","#BA72D3")

想法是用這兩個向量重現上面的腳本。

我試過了:

paste0("ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep(",vectors1,", 5),col = list(type1 = c(",vectors1,"=",vectors2,")))")

bu只能逐行粘貼,例如:

[1] "ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep(name1, 5),col = list(type1 = c(name1=#DCDCDC)))"                   
[2] "ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep(name2, 5),col = list(type1 = c(name2=#DC928B)))"
[3] "ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep(name3, 5),col = list(type1 = c(name3=#BA72D3)))"       

而不是做我想要的...

有人有主意嗎?

謝謝你的時間。

將代碼構建為字符串通常不是一個好主意。 而是考慮構建一個函數來執行您想要的操作。

你可以做點事情

ha_column_fun = function(names, colors) {
    HeatmapAnnotation(
      df = data.frame(type1 = rep(names, each=5)), 
      col = list(type1=setNames(colors, names))
    )
}

然后你可以用

ha_column = ha_column_fun(vectors1, vectors2)

暫無
暫無

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

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