Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R脚本中多次使用load()_R - Fatal编程技术网

在R脚本中多次使用load()

在R脚本中多次使用load(),r,R,我正在批处理模式下运行这个R脚本。这4个文件路径由shell脚本作为命名字符串(inPath1…inPath3,outPath)传入。以下是shell脚本: #!/bin/bash R CMD BATCH --no-save --no-restore '--args inPath1="~/backtest/results/aapl-RESULTS.RData" inPath2="~/backtest/results/adbe-RESULTS.RData"

我正在批处理模式下运行这个R脚本。这4个文件路径由shell脚本作为命名字符串(inPath1…inPath3,outPath)传入。以下是shell脚本:

#!/bin/bash

R CMD BATCH --no-save --no-restore '--args inPath1="~/backtest/results/aapl-RESULTS.RData" 
                inPath2="~/backtest/results/adbe-RESULTS.RData" 
                inPath3="~/backtest/results/adi-RESULTS.RData" 
                outPath="~/backtest/profitBreakdown.csv' ~/Desktop/derp.R ~/Desktop/script.out
下面是R脚本:

library(xts)

args <- commandArgs(trailingOnly=TRUE)

for(i in 1:3){
  eval(parse(text=args[i]))
  pathName <- paste("inPath", i, sep = "")
  load(pathName)  
}

为什么会这样?文件和目录存在。我可以双击它们,然后用鼠标将它们载入。每次使用load()后是否必须关闭连接?

您认为您可以努力使其可复制吗?调试我们无法运行的代码很困难。使用
eval
看起来很危险:解析参数更安全,例如使用
strsplit
(或任何其他字符串操作函数)。您可以尝试在加载之前添加
stopifnot(file.exists(path))
,以确保该文件确实存在。@GSee--刚刚编辑过它。在不提供.RData文件的情况下,我认为这是我能达到的最接近再现性的问题。我几乎可以肯定,问题源于您不必要地使用
eval(parse(…)
。不要以
inPath1=…
的形式传递参数,而只需传递路径本身,并在需要时提取所需的路径。再想一想,虽然
eval(parse(…)
不好,但直接的问题是将
“inPath1”
传递到
load
。这是一个原始字符串,而不是文件的路径。阅读错误消息。
Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
Calls: load -> readChar
In addition: Warning message:
In readChar(con, 5L, useBytes = TRUE) : cannot open compressed file 'inPath1', probable reason 'No such file or directory'