使用snowfall :: sfLapply时正在处理哪个列表元素? [英] which list element is being processed when using snowfall::sfLapply?

查看:114
本文介绍了使用snowfall :: sfLapply时正在处理哪个列表元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个列表(mylist)用作lapply函数的输入对象.有没有办法知道正在评估mylist中的哪个元素?该方法也应适用于lapplysnowfall::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))

因此,我们具有以下结构:

So we have the following structure:

> 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

可能在降雪方面起作用.

Which might work on snowfall.

这篇关于使用snowfall :: sfLapply时正在处理哪个列表元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆