本文介绍了tryCatch无法捕获RStudio中install.packages产生的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下用法:

tryCatch(log("a"), error = function(e) NULL)
#NULL

现在,我正在尝试执行基本相同的操作,但是方式更加复杂.我有两个网络存储库,如果第一个由于某种原因不可用,我想从第二个安装软件包.这是我的方法:

Now I'm trying to do essentially the same, but in a more complicated fashion. I have two network repositories, and I'd like to install packages from the second if the first is not available for some reason. Here's how I do it:

pkg_location <- c("file://main_repo", "file://extra_repo")
lapply(pkg_location, function(repo)
{
  tryCatch(install.packages("my-cool-package",
                            contriburl = repo, dependencies = TRUE),
           error = function(e) NULL)
})

我希望有一个NULL的列表.但是,该错误不会被抑制:

And I'm expecting a list of NULLs. However, the error is not suppressed:

Installing package into ‘...’
(as ‘lib’ is unspecified)
Warning in install.packages :
  cannot open compressed file '//extra_repo/PACKAGES',
    probable reason 'No such file or directory'
Error in install.packages : cannot open the connection
[[1]]
NULL

[[2]]
NULL

似乎install.packages某种程度上忽略了该机制.那怎么可能,为什么会发生?我该如何解决这个问题?

It seems like install.packages somehow ignores the mechanism. How is that possible, why is that happening and how can I approach the problem?

这里是sessionInfo,可能值得注意的是我正在运行RStudio 0.98.977.

Here's sessionInfo, probably worth noting I'm running RStudio 0.98.977.

> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_3.1.2

推荐答案

RStudio不会执行正常的install.packages,而是执行其自身的操作:

RStudio does not exectute the normal install.packages but instead does its own thing:

查看RStudio中的代码:

look at the code in RStudio:

> install.packages
function (...)
.rs.callAs(name, hook, original, ...)
<environment: 0x3e4b478>
> .rs.callAs
function (name, f, ...)
{
    withCallingHandlers(tryCatch(f(...), error = function(e) {
        cat("Error in ", name, " : ", e$message, "\n", sep = "")
    }), warning = function(w) {
        cat("Warning in ", name, " :\n  ", w$message, "\n", sep = "")
        invokeRestart("muffleWarning")
    })
}
<environment: 0x3bafa38>

奇怪的代码,它使自己回想起...我期待在某个地方找到.Primitive()

weird code, it recalls itself ...i was expecting a .Primitive() somewhere

> sum
function (..., na.rm = FALSE)  .Primitive("sum")

但这是一个丑陋的RStudio骇客.如果您以普通的R语言查看install.packages,则会得到:

but it is an ugly RStudio hack. if you look at install.packages in normal R you get:

这篇关于tryCatch无法捕获RStudio中install.packages产生的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 07:31
查看更多