0

也许这是一件微不足道的事情,我只是在同一个代码中寻找太久......当getFLOSSmoleDataXML.R通过 RStudio 采购 R 模块时,代码正确检测目录中的 .Rdata 文件cache并跳过下载和解析阶段。另一方面,当 R 通过 GNU make( sudo -u ruser make) 处理相同的模块时,结果很奇怪:

Rscript --no-save --no-restore --verbose getFLOSSmoleDataXML.R
running
  '/usr/lib/R/bin/R --slave --no-restore --no-save --no-restore --file=getFLOSSmoleDataXML.R'

Loading required package: RCurl
Loading required package: methods
Loading required package: bitops
Loading required package: XML
Loading required package: digest

Verifying repository: FreeCode

Checking file "http://flossdata.syr.edu/data/fc/2013/2013-Dec/fcProjectAuthors2013-Dec.txt.bz2"...

rdataFile = "./cache/5802dbd08ebefadf70fbb826776f9f0f.Rdata"...

trying URL 'http://flossdata.syr.edu/data/fc/2013/2013-Dec/fcProjectAuthors2013-Dec.txt.bz2'
Content type 'application/x-bzip2' length 514960 bytes (502 Kb)
opened URL
==================================================
downloaded 502 Kb

Error in gzfile(file, "wb") : cannot open the connection
Calls: print ... FUN -> importRepoFiles -> lapply -> FUN -> save -> gzfile
In addition: Warning message:
In gzfile(file, "wb") :
  cannot open compressed file './cache/5802dbd08ebefadf70fbb826776f9f0f.Rdata', probable reason 'No such file or directory'
Timing stopped at: 0.74 0.068 1.134
Execution halted
make[1]: *** [importFLOSSmole] Error 1
make[1]: Leaving directory `/home/ruser/diss-floss/import'
make: *** [collection] Error 2
ubuntu@ip-10-164-108-61:/home/ruser/diss-floss$ ls -l cache/5802*
-rw-r--r-- 1 ruser ruser 1968939 Feb 19 05:47 cache/5802dbd08ebefadf70fbb826776f9f0f.Rdata

正如您从最后两行看到的,我验证并确认该文件确实存在。这里发生了什么?有什么想法或建议吗?谢谢!

4

1 回答 1

1

经过简短的调查,我自己找到了这个问题的根源。正如我所料,这确实是一个简单而小错误,我将描述它以防止其他人碰到类似的事情。

当我file.exists()在我的代码中使用时,我将相关文件的相对路径作为参数传递。我通过连接硬编码的“缓存”目录和动态确定的文件名本身来构造该路径:

# calculate URL's digest and generate corresponding RData file name
fileDigest <- digest(url, algo="md5", serialize=F)
rdataFile <- paste(RDATA_DIR, "/", fileDigest, RDATA_EXT, sep = "")

但是,我忘记了make离开顶级项目目录并进入子目录来构建代码,因此“缓存”目录的相对路径RDATA_DIR="./cache"的硬编码值 ( ) 变得不正确。简单的更改 ( RDATA_DIR="../cache") 解决了问题。

这就解释了“魔法”背后的原因:-),当相同的代码手动成功构建(R 或 RStudio),但在通过make. 话虽如此,我认识到这可能不是依赖预定目录结构的最佳实践,但由于时间限制,我必须做出妥协(并将项目添加到 TODO [潜在改进] 列表)。我很乐意听取您对该领域最佳实践的建议。

于 2014-02-21T02:24:08.227 回答