'How to pass unknown variables to another function in R
I am writing a function, which generates a Heatmap using some input data, but performs some transformations beforehand. To generate the actual heatmap, I am using ComplexHeatmap::Heatmap, which offers a huge variety of options to modify the heatmap. I would like to give the user the opportunity to directly specify arguments of the ComplexHeatmap::Heatmap through my functions.
Right now I am using something which looks like this:
library(ComplexHeatmap)
mat = matrix(rnorm(100), 10)
rownames(mat) = paste0("R", 1:10)
colnames(mat) = paste0("C", 1:10)
test_fun <- function(mat, ...){
args = list(...)
print(args)
p <- ComplexHeatmap::Heatmap(mat,
show_row_names = args$show_row_names)
return(p)
}
test_fun(mat, show_row_names=F)
This works fine, but lets say if the user wants to change the show_column_names argument of ComplexHeatmap::Heatmap. This would result in an error because I did not explicitly defined args$show_column_names. It seems very tedious to do this manually for every single argument that ComplexHeatmap::Heatmap offers and I was wondering if there is a more elegant way to do this. Ideally and what I ultimately would like is, that ComplexHeatmap::Heatmap would use its default argument's, but the user can "overwrite" them if he/she specifies them in the test_fun function.
I thought something like this might work, but unfortunately it doesnt.
test_fun <- function(mat, ...){
args = list(...)
print(args)
p <- ComplexHeatmap::Heatmap(mat, ...)
return(p)
}
test_fun(mat, show_row_names=F, show_column_names=F)
Any help is much appreciated!
Solution 1:[1]
I'm not particularly familiar with the package, but do you just want to:
test_fun <- function(mat, ...){
ComplexHeatmap::Heatmap(mat,...)
}
It seems like you do, for example we could get rid of the column names and the row_names by calling test_fun as so:
test_fun(mat, show_row_names=FALSE, show_column_names = FALSE)
The ellipsis or ... allows us to place an arbitrary number of 'arguments recognised by the ComplexHeatmap::Heatmap function' in our test_fun function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
