使用 Snowfall::sfLapply 时正在处理哪个列表元素?

发布于 2024-10-02 06:50:26 字数 821 浏览 0 评论 0原文

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左耳近心 2024-10-09 06:50:26

我认为您将不得不在该聊天会话中使用谢恩的解决方案/建议。将对象存储在列表中,使得顶部列表的每个组件都包含一个具有该列表组件中包含的名称或 ID 或实验的组件,以及一个包含要处理的对象的组件:

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

:类似于以下函数中的第一行,后面是 Which

foo <- function(x) {
    writeLines(paste("Processing Component:", x$ID))
    sum(x$obj)
}

will do this:

> res <- lapply(obj, foo)
Processing Component: 1
Processing Component: 2
Processing Component: 3
Processing Component: 4
Processing Component: 5

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:

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)
}

Which will do this:

> res <- lapply(obj, foo)
Processing Component: 1
Processing Component: 2
Processing Component: 3
Processing Component: 4
Processing Component: 5

Which might work on snowfall.

﹉夏雨初晴づ 2024-10-09 06:50:26

我也可以像这样改变属性。

mylist <- list(a = 1:10, b = 1:10)
attr(mylist[[1]], "seq") <- 1
attr(mylist[[2]], "seq") <- 2

foo <- function(x) {
    writeLines(paste("Processing Component:", attributes(x)))   
}
bar <- lapply(mylist, FUN = foo)

(以及并行版本)

mylist <- list(a = 1:10, b = 1:10)
attr(mylist[[1]], "seq") <- 1
attr(mylist[[2]], "seq") <- 2

foo <- function(x) {
    x <- paste("Processing Component:", attributes(x))  
}
sfExport("mylist", "foo")
bar <- sfLapply(mylist, fun = foo)

I could also alter the attributes like so.

mylist <- list(a = 1:10, b = 1:10)
attr(mylist[[1]], "seq") <- 1
attr(mylist[[2]], "seq") <- 2

foo <- function(x) {
    writeLines(paste("Processing Component:", attributes(x)))   
}
bar <- lapply(mylist, FUN = foo)

(and the parallel version)

mylist <- list(a = 1:10, b = 1:10)
attr(mylist[[1]], "seq") <- 1
attr(mylist[[2]], "seq") <- 2

foo <- function(x) {
    x <- paste("Processing Component:", attributes(x))  
}
sfExport("mylist", "foo")
bar <- sfLapply(mylist, fun = foo)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文