我正在尝试使用foreach从目录中读取数据文件。但它给了我一个错误。它适用于我的办公室机器,但在家里不起作用。两台机器都有4个核心,底部检查输出。这是代码
rm(list=ls())
setwd("D:/Test")
library(foreach)
library(doParallel)
c1<-makeCluster(4, outfile = "debug.txt")
CE<-clusterEvalQ(c1, .libPaths(""))
registerDoParallel(c1)
print(paste0("Cores = ",detectCores()))
file.names <- dir(pattern ="h00|B00")
output<-list()
output<-foreach (i=1:4) %dopar% {
read.table(file=file.names[i])
}
stopCluster(c1)
我收到错误:
错误{:任务1失败 - “无法打开连接”
答案 0 :(得分:0)
import os
from cx_Freeze import setup, Executable
os.environ['TCL_LIBRARY'] = 'c:/python36/tcl/tcl8.6'
os.environ['TK_LIBRARY'] = 'c:/python36/tcl/tk8.6'
buildOptions = dict(
packages = [],
excludes = [],
include_files=['c:/python36/DLLs/tcl86t.dll', 'c:/python36/DLLs/tk86t.dll']
)
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('editor.py', base=base)
]
setup(name='editor',
version = '1.0',
description = '',
options = dict(build_exe = buildOptions),
executables = executables)
答案 1 :(得分:0)
我不是R中并行操作的专家,我无法帮助您解决为什么您在不同的机器上有不同的行为(它们是相同的操作系统,具有相同的版本R和R包?)。我的理解是像foreach
这样的函数在后台启动多个R会话,每个会话充当一个&#34;节点&#34;计算操作的子集。在您的情况下,每个节点都需要查找要提供read.table
的文件,因此我个人认为在使用并行进程时传递完整文件路径是一种很好的做法。
使用带有默认参数的dir
返回相对文件路径(即:相对于当前工作目录),这意味着您需要保留在当前工作目录中以正确引用它们。我将首先将我的工作目录设置为我的桌面来解释:
desktop.path <- "~/Desktop"
setwd( desktop.path )
getwd()
# [1] "/Users/ross/Desktop"
现在我们可以获得一个&#34; .txt&#34;文件,以某种方式位于我的桌面上。首先,使用默认参数。
file.default <- dir( pattern = "txt" )
file.default
# [1] "CW_denseCloud_LowestQual_withGCPs.txt"
请注意,该链接中没有任何内容可以显示文件的位置,我们依靠当前的工作目录来查找文件,现在没问题。
file.exists( file.default )
[1] TRUE
但如果我们最终进入另一个工作目录,我们将丢失该文件:
setwd( "~" )
file.exists( file.default )
# [1] FALSE
如果我们传递参数full.names = TRUE
,我们获得的不仅仅是文件名本身,但它仍然是一个相对路径,这对我们没有帮助:
setwd( desktop.path )
dir( pattern = "txt", full.names = TRUE )
# [1] "./CW_denseCloud_LowestQual_withGCPs.txt"
有助于将完整路径传递给dir
,以便dir
有效地查看相对于根目录的文件,而不是从当前工作目录:
file.full <- dir( path = desktop.path, pattern = "txt", full.names = TRUE )
file.full
# [1] "/Users/ross/Desktop/CW_denseCloud_LowestQual_withGCPs.txt"
现在我们已经找到了完整的文件路径,而不是相对文件,这意味着无论我们在哪里(工作目录),我们都会找到这个文件:
file.exists( file.full )
# [1] TRUE
setwd( "~" )
file.exists( file.full )
# [1] TRUE
现在,即使工作目录没有正确传递到每个处理节点,他们仍然可以找到他们需要的文件。