简体   繁体   中英

tryCatch not catching error produced by install.packages within RStudio

Consider the following usage:

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)
})

And I'm expecting a list of NULL s. 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

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

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 does not exectute the normal install.packages but instead does its own thing:

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>

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

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

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

head(install.packages) # it is really long :P
1 function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos, 2 type), method, available = NULL, destdir = NULL, dependencies = NA,
3 type = getOption("pkgType"), configure.args = getOption("configure.args"),
4 configure.vars = getOption("configure.vars"), clean = FALSE,
5 Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
6 libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
....

I'm going to suggest closing as off-topic because this is an RStudio problem. Basically, tryCatch is catching the error, but RStudio's error handler prints the error anyway. Thus the reason you're getting a return value:

[[1]]
NULL

[[2]]
NULL

This means tryCatch works. RStudio just prints caught errors weirdly.

使用命名空间调用:

utils::install.packages()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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