繁体   English   中英

R:为什么 fs::dir_copy 有时会失败?

[英]R: Why does fs::dir_copy fail some of the time?

在一个过程中,我需要使用fs::dir_copy(currentPath, newPath)复制一个目录。 有时,我收到以下错误消息:

Error in link_copy(links, path(new_path[[i]], path_rel(links, path[[i]])), : all(is_link(path)) is not TRUE

这只在某些时候发生,并非总是如此。 更重要的是,如果我用自己的手动副本替换我的目录(即手动复制目录,删除原始目录,重命名副本),那么我的代码将起作用。

有人可以解释为什么会发生这种情况吗? 有没有办法一劳永逸地避开这个错误?

这不能回答您的问题,但可以帮助您进一步分析正在发生的事情。 R 的一大优点是您可以轻松检查源代码。 尤其是在出现异常情况的情况下,查看代码可能很有用。

在您的情况下,我们可以检查fs::dir_copy的源代码并追溯生成错误消息的代码。 If you type fs::dir_copy (no parenthesis) into the console, R will print the R code of the function (if the function is not primitive). 这样做会告诉你fs:dir_copy function 调用fs::link_copy function。 有道理,因为错误消息来自此 function。 我们现在可以使用fs::link_copy打印此 function 此 function 在此行中生成错误消息:

stopifnot(all(is_link(path)))

从错误消息中我们知道all(is_link(path))返回FALSE 下一步是查看fs::is_link function。 Here we see that the error may come from the call to the setNames function, which depends on the fs::file_info function: res <- file_info(path) Here we see that setNames is called with a condition depending on what the file_info function returned :

setNames(!is.na(res$type) & res$type == "symlink", res$path)

这看起来不寻常,因为setNames采用 object 和字符向量作为形式。 但是,我不是这些函数的开发者;-) 也许您的机器上的文件类型存在问题,并且fs::file_info(path)在某些情况下会返回意外的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM