R群集导出错误“未找到对象"; [英] R Cluster export error "object not found"

查看:256
本文介绍了R群集导出错误“未找到对象";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我理解为什么我的程序会产生此错误吗?

Can someone please help me understand why my program produces this error?

从这里可以看出,"pay.freq"显然是环境的一部分,那么为什么找不到它呢?语法与"ts"一样,可以毫无问题地找到

As can be seen here "pay.freq" clearly is part of the environment, so why can it not find it? Syntax is the same as for "ts" which it can find without problems

大圆圈部分覆盖了单词功能,小圆圈部分覆盖了单词情节.

Big circle partly cvovers the word function, small circle partly covers the word plot.

cf.pro <- function(t=0,Tb=T,r=Y, k=1, PRFlag="P", freq="w",plot=0){ #Beregner exposure for alle tidspunkter med udgangspunkt 
  ts <- seq(0,30,1/52)
  pay.freq <- if(toupper(freq)=="W"){1}else #bestemmer hvor ofte der sker betalinger
    if(toupper(freq)=="Q"){13}else
      if(toupper(freq)=="H"){26}else
        if(toupper(freq)=="Y"){52}else print("Fejl i frequency input")

  library('parallel')
  cl <- makeCluster(7)
  clusterEvalQ(cl,source("C:/Users/Marcus/Documents/CBS/Speciale/Data/Global data.R"))
  clusterEvalQ(cl,source("C:/Users/Marcus/Documents/CBS/Speciale/Data/Swappriser.R"))
  clusterEvalQ(cl,source("C:/Users/Marcus/Documents/CBS/Speciale/Data/Interest simulation.R"))
  clusterEvalQ(cl,source("C:/Users/Marcus/Documents/CBS/Speciale/Data/Survival sim.R"))
  clusterEvalQ(cl,source("C:/Users/Marcus/Documents/CBS/Speciale/Data/Exposures.R"))
  clusterExport(cl,"ts")
  clusterExport(cl,"pay.freq")

 cf.pro <- parSapplyLB(cl,1:n, function(j){ #Beregner exposure serie n gange
    if (k==1) k=Swap(t=0,Ta=0,Tb=Tb,r=r[,j])
    sapply(ts,function(i){Exposure.cf(t=i,Tb=Tb,r=r[,j], k=k, PRFlag=PRFlag, pay.freq=pay.freq)}) #beregner exposure for alle tidspunkter
  })
  stopCluster(cl)

  if (plot==1) {
    tss <- seq(t, Tb, dt)
    matplot(tss, cf.pro[,1:n], type="l", lty=1, main="Exposure Profiles", ylab="Exposure") 
    lines(tss,rowMeans(cf.pro), lty=1, lwd=3)
  } 
  return(cf.pro)
}

CF.pro.w=cf.pro(t=0,Tb=T,r=r, PRFlag="P", freq="w", plot=1)

推荐答案

如果您查看clusterExport文档,则调用如下

If you take a look at the clusterExport documentation the call is the following

clusterExport(cl, varlist, envir = .GlobalEnv)

如您所见,查找要导出的变量的默认环境是.GlobalEnv.

As you can see, the default environment to look for the variable you are trying to export is .GlobalEnv.

您正在函数中进行导出,并且pay.freq的范围不是GlobalEnv,而是函数的本地环境.但是,您尚未将函数的环境指定为clusterExport,因此clusterExport会查找GlobalEnv,而找不到pay.freq.

You are doing the export within the function, and the scope of pay.freq is not GlobalEnv but the local environment of the function. Yet you haven't specified the environment of the function to clusterExport, so clusterExport looks to GlobalEnv and doesn't find pay.freq.

我敢打赌这是您的问题,并且pay.freq现在出现在您的环境中,因为您可能逐行通过代码进行测试.我将清除您的环境,然后通过将函数环境指定为clusterExport来尝试再次运行代码.

I'm willing to bet this is your problem, and that pay.freq shows up in your environment now because you probably went line by line through your code to test. I'd clear your environment and try to run the code again by specifying the function environment to clusterExport.

让我知道这是怎么回事,如果问题更加细微,我们也许可以解决.这只是我首先想到的问题.

Let me know how that goes, and we can maybe work through it if the problem is a bit more nuanced. This was just my first thought on looking at the question.

这篇关于R群集导出错误“未找到对象";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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