install.packages() ignoring option to convert warning to error?
Asked Answered
S

1

3

I'm trying to find a simple way to make install.packages() throw an error if it fails (rather than just a warning).

What I've tried

Setting options(warn=2) converts warnings into errors. Example:

options(warn=2)
warning()
# Error: (converted from warning) 

I expected this would now error:

install.packages('thispackagedoesntexist')
# Warning in install.packages :
#   package ‘thispackagedoesntexist’ is not available for this version of R
#
# A version of this package for your version of R might be available elsewhere,
# see the ideas at
# https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages

yet it still just gives a warning (no error).

Question

How can install.packages() be made to error (rather than simply warn) on any sort of failure?


Note:

  • There are a few nice ways of making install.packages() error instead of warning, but I'm scouting for something much more lightweight, preferably without installing other packages, which options() would achieve nicely (if I can get it working).
Sacramentalist answered 25/11, 2022 at 4:50 Comment(0)
P
2

This is an RStudio "feature".

First I wondered why the warning isn't printed in red. Then I looked at install.packages (in RStudio) and saw this:

> install.packages
function (...) 
.rs.callAs(name, hook, original, ...)
<environment: 0x1408432c8>

> getAnywhere(.rs.callAs)
A single object matching ‘.rs.callAs’ was found
It was found in the following places
  tools:rstudio
with value

function (name, f, ...) 
{
    withCallingHandlers(tryCatch(f(...), error = function(e) {
        cat("Error in ", name, " : ", e$message, "\n", sep = "")
    }), warning = function(w) {
        if (getOption("warn") >= 0) 
            cat("Warning in ", name, " :\n  ", w$message, "\n", 
                sep = "")
        invokeRestart("muffleWarning")
    })
}
<environment: 0x1181b4928>

See how warnings are handled and how the printed "warning" isn't actually a warning but cat output?

If I run your code in Rgui, I see this:

> options(warn=2)
> install.packages('thispackagedoesntexist')
Error: (converted from warning) package ‘thispackagedoesntexist’ is not available for this version of R

A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages

So, I suggest you go to the RStudio developers and complain that they use cat to misleadingly print "errors" and "warnings".

You can avoid RStudio's masking of install.packages the usual way:

> options(warn=2)
> utils::install.packages('thispackagedoesntexist')
Error: (converted from warning) package ‘thispackagedoesntexist’ is not available for this version of R

A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
Phonemic answered 25/11, 2022 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.