从字符向量中创建函数的参数列表
Make a list of arguments for a function from a character vector
我正在制作一个 ComplexHeatmap,我想显示与原始数据中的信息列相对应的多行注释(即 rowAnnotation 函数内的多个命名参数)。我现在在做什么,有7列信息:
library(ComplexHeatmap)
# sample dataframe
EF_data <- data.frame(Package = sample(1:10, 10),
Information = sample(1:10, 10),
Atoms = sample(1:10, 10),
Measure = sample(1:10, 10),
Filter = sample(1:10, 10),
Analysis = sample(1:10, 10),
Averaging = sample(1:10, 10))
# what i'm doing now
left_an_gp <- gpar(fontsize = 8)
left_an = rowAnnotation(Package = anno_text(EF_data$Package, gp = left_an_gp),
Information = anno_text(EF_data$Information, gp = left_an_gp),
Atoms = anno_text(EF_data$Atoms, gp = left_an_gp),
Measure = anno_text(EF_data$Measure, gp = left_an_gp),
Filter = anno_text(EF_data$Filter, gp = left_an_gp),
Analysis = anno_text(EF_data$Analysis, gp = left_an_gp),
Averaging = anno_text(EF_data$Averaging, gp = left_an_gp),
gap = unit(0.5, "mm"))
# and then draw the heatmap with the data from a matrix etc
我想要做的是使用例如 columns(EF_data) 字符向量生成 rowAnnotation。 rowAnnotate 中的每个参数都是相同的,除了它的名称和 $ 之后的列的名称,所以我想知道是否有更压缩的方法来执行此操作。我曾尝试寻找一种方法,但我想我没有使用正确的术语来找到一种方法。
我想应该是这样的:
apply(
eval(
"{} = anno_text(EF_data${}, gp = left_an_gp)"
),
columns(EF_data)
)
但我什至不知道如何开始尝试正确地做到这一点。
您无需求助于解析和评估文本。相反,您可以使用 lapply
构建调用的参数并使用 do.call
调用函数
args <- lapply(EF_data, function(x) row_anno_text(x, gp = left_an_gp))
args <- setNames(args, names(EF_data))
args$gap <- unit(0.5, "mm")
left_an <- do.call(rowAnnotation, args)
left_an
#> A HeatmapAnnotation object with 7 annotations
#> name: heatmap_annotation_0
#> position: row
#> items: 10
#> width: 24.9681777777778mm
#> height: 1npc
#> this object is subsettable
#>
#> name annotation_type color_mapping width
#> Package anno_text() 3.13831111111111mm
#> Information anno_text() 3.13831111111111mm
#> Atoms anno_text() 3.13831111111111mm
#> Measure anno_text() 3.13831111111111mm
#> Filter anno_text() 3.13831111111111mm
#> Analysis anno_text() 3.13831111111111mm
#> Averaging anno_text() 3.13831111111111mm
由 reprex package (v2.0.1)
于 2022-05-09 创建
我正在制作一个 ComplexHeatmap,我想显示与原始数据中的信息列相对应的多行注释(即 rowAnnotation 函数内的多个命名参数)。我现在在做什么,有7列信息:
library(ComplexHeatmap)
# sample dataframe
EF_data <- data.frame(Package = sample(1:10, 10),
Information = sample(1:10, 10),
Atoms = sample(1:10, 10),
Measure = sample(1:10, 10),
Filter = sample(1:10, 10),
Analysis = sample(1:10, 10),
Averaging = sample(1:10, 10))
# what i'm doing now
left_an_gp <- gpar(fontsize = 8)
left_an = rowAnnotation(Package = anno_text(EF_data$Package, gp = left_an_gp),
Information = anno_text(EF_data$Information, gp = left_an_gp),
Atoms = anno_text(EF_data$Atoms, gp = left_an_gp),
Measure = anno_text(EF_data$Measure, gp = left_an_gp),
Filter = anno_text(EF_data$Filter, gp = left_an_gp),
Analysis = anno_text(EF_data$Analysis, gp = left_an_gp),
Averaging = anno_text(EF_data$Averaging, gp = left_an_gp),
gap = unit(0.5, "mm"))
# and then draw the heatmap with the data from a matrix etc
我想要做的是使用例如 columns(EF_data) 字符向量生成 rowAnnotation。 rowAnnotate 中的每个参数都是相同的,除了它的名称和 $ 之后的列的名称,所以我想知道是否有更压缩的方法来执行此操作。我曾尝试寻找一种方法,但我想我没有使用正确的术语来找到一种方法。
我想应该是这样的:
apply(
eval(
"{} = anno_text(EF_data${}, gp = left_an_gp)"
),
columns(EF_data)
)
但我什至不知道如何开始尝试正确地做到这一点。
您无需求助于解析和评估文本。相反,您可以使用 lapply
构建调用的参数并使用 do.call
args <- lapply(EF_data, function(x) row_anno_text(x, gp = left_an_gp))
args <- setNames(args, names(EF_data))
args$gap <- unit(0.5, "mm")
left_an <- do.call(rowAnnotation, args)
left_an
#> A HeatmapAnnotation object with 7 annotations
#> name: heatmap_annotation_0
#> position: row
#> items: 10
#> width: 24.9681777777778mm
#> height: 1npc
#> this object is subsettable
#>
#> name annotation_type color_mapping width
#> Package anno_text() 3.13831111111111mm
#> Information anno_text() 3.13831111111111mm
#> Atoms anno_text() 3.13831111111111mm
#> Measure anno_text() 3.13831111111111mm
#> Filter anno_text() 3.13831111111111mm
#> Analysis anno_text() 3.13831111111111mm
#> Averaging anno_text() 3.13831111111111mm
由 reprex package (v2.0.1)
于 2022-05-09 创建