首页 > 解决方案 > 为什么未来的包找不到我的功能?

问题描述

我今天早些时候问了一个类似的问题,但@MrFlick 发现我的可重现示例存在问题。现在我有另一个可重现的例子,我认为这个有效。

问题是未来的包看不到我的功能之一。我也尝试过 foreach 并且它有同样的问题:它看不到相同的功能。所以这里是代码:

library("future")
library("tidyverse")
library("rlist")

aspect.major.names <- c("Conjunction", "Opposition", "Trine", "Square", "Sextile")

aspect_strength <- function(angle, center=0, orb=8) {
  abs_angle <- abs(angle)
  calculated_angle <- if (abs_angle >= center - orb && abs_angle <= center + orb) {
    abs_angle
  } else {
    abs_angle_360 <- 360 - abs_angle
    if (abs_angle_360 >= center - orb && abs_angle_360 <= center + orb) {
      abs_angle_360} else {-1}
  }
  if (calculated_angle < 0) {return(0)}

  1 - abs(calculated_angle - center)/orb
}

conjunction_func <- function(x) aspect_strength(x, 0, conjunction_orb)
conjunction_strength <- function(x) sapply(x, conjunction_func)
sextile_func <- function(x) aspect_strength(x, 60, sextile_orb)
sextile_strength <- function(x) sapply(x, sextile_func)
square_func <- function(x) aspect_strength(x, 90, square_orb)
square_strength <- function(x) sapply(x, square_func)
trine_func <- function(x) aspect_strength(x, 120, trine_orb)
trine_strength <- function(x) sapply(x, trine_func)
opposition_func <- function(x) aspect_strength(x, 180, opposition_orb)
opposition_strength <- function(x) sapply(x, opposition_func)

strength_funcs <- c(conjunction_strength, opposition_strength,
                    trine_strength, square_strength, sextile_strength)
names(strength_funcs) <- aspect.major.names

smoa <- c(15.402917, 29.143750, 42.892500, 56.533333, 69.970417, 83.142083, 96.012500, 108.580000, 120.861667, 132.891667)
smea <- c(339.32542, 339.63667, 339.96375, 340.30792, 340.67208, 341.04917, 341.44333, 341.85042, 342.26750, 342.70167)
df_angles <- data.frame(Sun_Moon_Angle = smoa, Sun_Mercury_Angle = smea)

orb <- 8
conjunction_orb <- sextile_orb <- square_orb <- trine_orb <- opposition_orb <- orb

csma <- function(asp) {

  # This is needed otherwise there is an error
  # conjunction_func
  # opposition_func
  # trine_func
  # square_func
  # sextile_func

  df_angles %>%
    apply(2, strength_funcs[[asp]]) %>%
    as.data.frame() -> df_strength
  df_strength
}

plan(sequential)
plan(multisession, workers=6)
futs <- lapply(aspect.major.names, function(x) future({ csma(x) }))
lapply(futs, value) %>% list.cbind() -> dfsmas

当我运行此代码时,我收到以下消息:

lapply(futs, value) %>% list.cbind() -> dfsmas
Error in match.fun(FUN) : object 'conjunction_func' not found

第一个 lapply 执行没有问题。正是在调用值函数的第二个函数中发生了错误。

奇怪的是(至少对我而言)是,如果在 csma 函数中取消注释这些行,则代码可以正常工作并且没有错误。

  # conjunction_func
  # opposition_func
  # trine_func
  # square_func
  # sextile_func

未注释,这些行似乎提醒未来这些功能存在。

标签: rparallel-processingfutureparallel.foreach

解决方案


推荐阅读