I am trying to repeatedly call a function (specifically Seurat::DimPlot), where one of the the arguments is a named list (cells.highlight). I am planning to use purrr::imap
to do make the call. I have a named list, where each of the element of the list is a vector of characters. I would like to create the list for the cells.highlight argument of Seurat::DimPlot. When I tried
imap(mylist, ~ DimPlot(seurat_obj, cells.highlight=list(.y=.x), cols="lightgrey",
cols.highlight="dodgerblue") )
The plot looks nice, but the instead of replacing the variable .y with the list element name, it leaves the list element name as ".y". (So on the plot I see ".y" in the legend). To Make a SSCE
dp <- function(name, values){
list(name = values)
}
dp("a",paste0("a",1:3))
# $name
# [1] "a1" "a2" "a3"
where I want it to be
$a
[1] "a1" "a2" "a3"`
I've tried using enquo, as_name, using ':=', ensym, quote, but I just can't seem to get it right. I know you can break this into two lines with something like list[[name]]=..., but because this is part of a larger problem, I would love to learn how to appropriately quote and unquote the variable name.
I've always had trouble using NSE, even after reading, re-reading, and re-reading Hadley Wickhams' Advanced-R chapter on quasiquotations and his chapter on evaluation, but it appears that all the pieces are there, I just can't put them together
Since you are usinr purrr
you should use set_names
to set the names of your list
imap(mylist, ~ DimPlot(seurat_obj, cells.highlight=set_names(list(.x), .y), cols="lightgrey",
cols.highlight="dodgerblue") )
With the dp
example, it would be
dp <- function(name, values){
set_names(list(values), name)
}
dp("a",paste0("a",1:3))
# $`a`
# [1] "a1" "a2" "a3"
So in this case this really doesn't have anything to do with non-standard evaluation. It's just about using the right function for the job.