HenrikBengtsson / startup

:wrench: R package: startup - Friendly R Startup Configuration
https://henrikbengtsson.github.io/startup/
163 stars 5 forks source link

options(error= ...) is overridden by RStudio Console #59

Closed csgillespie closed 6 years ago

csgillespie commented 6 years ago

If you set

options(error = X:Y)

RStudio removes it.

HenrikBengtsson commented 6 years ago

startup::startup() should detect this and give an informative warning that the option was set but then overridden by RStudio.

Is this happening only at the RStudio Console, or also in RStudio Terminals?

csgillespie commented 6 years ago

Sorry, what's the difference between Console/terminals ?

HenrikBengtsson commented 6 years ago

RStudio Console is what's been around since day one and what most people use. Recently, the added a Terminal, which "emulates" the classical R prompt (as if you ran it in a Linux terminal, say). It's available under the 'Tools' menu.

csgillespie commented 6 years ago

Just tested on a different machine, and error is respected in RStudio. I'll double check with my work machine next week

csgillespie commented 6 years ago

Under the terminal (both RStudio and standard linux),

R> is.null(options("error"))
[1] FALSE

However, running the same code in Rstudio console returns TRUE

R> RStudio.Version()$version
[1] '1.1.453'

R> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.4 LTS

Matrix products: default
BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] colorout_1.1-2

loaded via a namespace (and not attached):
[1] compiler_3.4.4      pillar_1.2.2        rematch2_2.0.1     
[4] tibble_1.4.2        rlang_0.2.0         autoinst_0.0.0.9000
[7] fortunes_1.5-4     
HenrikBengtsson commented 6 years ago

Ok, so you're observing this in the R Studio Console only.

I've tried to reproduce the with R Studio 1.1.453 (same as yours) on Ubuntu 16.04 and on Windows 8.1 both running R 3.5.0. On Windows, I get:

> getOption("error")
(function () 
{
    .rs.recordTraceback(TRUE, 5, .rs.enqueueError)
})()

whereas in my .Rprofile I set it to:

> getOption("error")
(function () 
{
    utils::recover()
})()

What's interesting is that I fail to reproduce it on Ubuntu - there my ~/.Rprofile settings are indeed respected. This led me to find https://support.rstudio.com/hc/en-us/community/posts/202296047-error-recovery, which suggests that the default behavior of R Studio is controlled by what you've selected in menu 'Debug -> On Error' which has radio buttons [ ] 'Message Only', [ ] 'Error Inspector', and [ ] 'Break in Code'. It turns out that on my Ubuntu, none of these were selected. However, as soon as I select one of them, my Rprofile settings are overwritten by this. What's bad is that you cannot unset this in RStudio.

After some more digging, I found that this is controlled by the following entry:

> settings_file <- "~/.rstudio-desktop/monitored/user-settings/user-settings"
> grep("errorHandlerType", readLines(settings_file), value = TRUE)
[1] "errorHandlerType=\"2\""

The errorHandlerType setting takes values in {0, 1, 2} corresponding to the three menu alternatives. If you remove this line and restart R Studio, it's value become:

> grep("errorHandlerType", readLines(settings_file), value = TRUE)
[1] "errorHandlerType=\"3\""

In that case, there is no preselected option in that debug menu. Also, the error option is no longer overridden by R Studio.

I think there are two bugs in R Studio here: (1) it overrides what you set error to be in your Rprofile, and (2) you cannot unselect the 'Debug -> On Error' option. You'd might expect that 'Message Only' (= errorHandlerType="0") would do it, but that forces error = NULL (regardless what you do in Rprofile).

Would you mind inquiring about this with the RStudio folks?

csgillespie commented 6 years ago

Will do.

csgillespie commented 6 years ago

Sounds like the only option is to get startup to raise warning if people use error in their startup?

HenrikBengtsson commented 6 years ago

Yes, an informative warning could be added. Conceptually,

if (!is.null(getOption("error")) && is_rstudio_console() && is_rstudio_error_set()) {
   warning("Option 'error' was set during the R startup, but will be overridden when
            using the RStudio Console because RStudio is configured to override this
            (menu 'Debug' -> 'On Error').")
}
HenrikBengtsson commented 6 years ago

In startup (>= 0.10.0-9000), startup() will now detect this RStudio conflict and produce a warning:

CONFLICT: Option 'error' was set during the R startup, but this will be overridden by the RStudio
settings (menu 'Debug -> On Error') when using the RStudio Console. To silence this warning, set
option 'error' using 'if (!startup::sysinfo()$rstudio) options(error = ...)'. For further details on this issue,
see https://github.com/rstudio/rstudio/issues/3007