使用snowfall::sfLapply 时正在处理哪个列表元素? [英] which list element is being processed when using snowfall::sfLapply?
问题描述
假设我们有一个列表 (mylist
),用作 lapply
函数的输入对象.有没有办法知道正在评估 mylist
中的哪个元素?该方法应该适用于 lapply
和 snowfall::sfApply
(以及可能的其他应用家庭成员).
Assume we have a list (mylist
) that is use as input object for a lapply
function. Is there a way to know which element in mylist
is being evaluated? The method should work on lapply
and snowfall::sfApply
(and possible others apply family members) as well.
在聊天上,Gavin Simpson 建议了以下方法.这对 lapply
很有用,但对 sfApply
效果不佳.我想避免额外的包或摆弄列表.有什么建议吗?
On chat, Gavin Simpson suggested the following method. This works great for lapply
but not so much for sfApply
. I would like to avoid extra packages or fiddling with the list. Any suggestions?
mylist <- list(a = 1:10, b = 1:10)
foo <- function(x) {
deparse(substitute(x))
}
bar <- lapply(mylist, FUN = foo)
> bar
$a
[1] "X[[1L]]"
$b
[1] "X[[2L]]"
这是没有切割它的平行版本.
This is the parallel version that isn't cutting it.
library(snowfall)
sfInit(parallel = TRUE, cpus = 2, type = "SOCK") # I use 2 cores
sfExport("foo", "mylist")
bar.para <- sfLapply(x = mylist, fun = foo)
> bar.para
$a
[1] "X[[1L]]"
$b
[1] "X[[1L]]"
sfStop()
推荐答案
我认为您将不得不在该聊天会话中使用 Shane 的解决方案/建议.将您的对象存储在一个列表中,这样顶部列表的每个组件都包含一个具有该列表组件中包含的名称或 ID 或实验的组件,以及一个包含您要处理的对象的组件:
I think you are going to have to use Shane's solution/suggestion in that chat session. Store your objects in a list such that each component of the top list contains a component with the name or ID or experiment contained in that list component, plus a component containing the object you want to process:
obj <- list(list(ID = 1, obj = 1:10), list(ID = 2, obj = 1:10),
list(ID = 3, obj = 1:10), list(ID = 4, obj = 1:10),
list(ID = 5, obj = 1:10))
所以我们有以下结构:
> str(obj)
List of 5
$ :List of 2
..$ ID : num 1
..$ obj: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ :List of 2
..$ ID : num 2
..$ obj: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ :List of 2
..$ ID : num 3
..$ obj: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ :List of 2
..$ ID : num 4
..$ obj: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ :List of 2
..$ ID : num 5
..$ obj: int [1:10] 1 2 3 4 5 6 7 8 9 10
具有类似于以下函数中的第一行的内容,然后是您的
The have something like the first line in the following function, followed by your
foo <- function(x) {
writeLines(paste("Processing Component:", x$ID))
sum(x$obj)
}
哪个会这样做:
> res <- lapply(obj, foo)
Processing Component: 1
Processing Component: 2
Processing Component: 3
Processing Component: 4
Processing Component: 5
这可能适用于降雪.
这篇关于使用snowfall::sfLapply 时正在处理哪个列表元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!