使用 Snowfall::sfLapply 时正在处理哪个列表元素?
假设我们有一个列表 (mylist
),用作 lapply
函数的输入对象。有没有办法知道正在评估 mylist
中的哪个元素?该方法应该适用于 lapply
和 snowfall::sfApply
(以及其他可能适用的家庭成员)。
在聊天中,Gavin Simpson 建议了以下方法。这对于 lapply
非常有用,但对于 sfApply
则不太有效。我想避免额外的包裹或摆弄列表。有什么建议吗?
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]]"
这是并行版本,没有削减它。
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()
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.
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您将不得不在该聊天会话中使用谢恩的解决方案/建议。将对象存储在列表中,使得顶部列表的每个组件都包含一个具有该列表组件中包含的名称或 ID 或实验的组件,以及一个包含要处理的对象的组件:
因此,我们有以下结构
:类似于以下函数中的第一行,后面是 Which
will do this:
Which might work on Snowfall。
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:
So we have the following structure:
The have something like the first line in the following function, followed by your
Which will do this:
Which might work on snowfall.
我也可以像这样改变属性。
(以及并行版本)
I could also alter the attributes like so.
(and the parallel version)