Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injection Point to Override RStudio Hooks #4498

Closed
coatless opened this issue Mar 22, 2019 · 1 comment

Comments

@coatless
Copy link

commented Mar 22, 2019

I would like to be able to add a custom hook within hooking architecture / "tools:rstudio" that RStudio sets. I'm specifically making this request to obtain access to the install.packages() hook established after the R session was fully established. c.f.

.rs.registerReplaceHook("install.packages", "utils", function(original,
pkgs,
lib,
repos = getOption("repos"),
...)

The present hook produces:

get('install.packages', 'package:utils')
# function (...) 
# .rs.callAs(name, hook, original, ...)
# <environment: 0x7f92d7069ce8>

After testing different options, all of RStudio's hooks seem to be established after R's own startup procedure that runs through .Renviron, .Rprofile, and .Rhistory. As a result, there isn't a way to mask code or load a package automatically that won't be overridden by RStudio claimed functions.

It would be helpful to have an injection point after the initial delayed registration.

// now that all initialization code has had a chance to run we
// can register all external routines which were added to r::routines
// during the init sequence
r::routines::registerAll();

The use case for the injection is:

  • Disabling communication with CRAN/Global CDNs for secure environments
  • Better user feedback as it now silently errors or triggers a full error when a bogus CRAN path is specified.
  • Similar behavior to launching R from Terminal or GUI.
  • Some kind of environmental disable package installation feature option (instead of removing the package panel).

Desired behavior
r-build-fine

Slient error without overriding CRAN mirror

rstudio-hook-install-pkg-delay

Noisy error without masked function call when overriding with a bogus CRAN mirror

bogus-cran-mirror

To recreate the mask shown desired, I've created a .Rprofile file that contains:

.First = function() {
    ## Place package on search path
    library("utils")
    
    ## Unlock environment where the function/variable is found.
    unlockBinding("install.packages", as.environment("package:utils"))
    
    ## Intercept install packages with a custom function.
    base::assign("install.packages",
                 value = function(...) { cat("Done!\n")},
                 envir = as.environment("package:utils"))
    
    ## Close the environment
    lockBinding("install.packages", as.environment("package:utils"))
}

coatless added a commit to illinois-r/r-centos7-cbtf that referenced this issue Mar 22, 2019

Clarity of install.packages() shim functionality
- Emphasize this shim will only work in R GUI or terminal.
- RStudio overrides the `install.packages()` function with its own variant to keep its package panel up to date.
- The only way to modify RStudio's override is to have the user trigger it via a package load or function call after RStudio has been fully initialized.

For more details, see: rstudio/rstudio#4498

coatless added a commit to illinois-r/r-centos7-cbtf that referenced this issue Mar 23, 2019

Re-enable the local CRAN variant (#3)
* Rename to setup-centos7...

* Re-enable the local CRAN setup

* Directly use package name in environment modifications

Switches from using the package name as a symbol to having it remain as a character.

* Clarity of install.packages() shim functionality

- Emphasize this shim will only work in R GUI or terminal.
- RStudio overrides the `install.packages()` function with its own variant to keep its package panel up to date.
- The only way to modify RStudio's override is to have the user trigger it via a package load or function call after RStudio has been fully initialized.

For more details, see: rstudio/rstudio#4498

* Spacing
@jmcphers

This comment has been minimized.

Copy link
Member

commented Mar 27, 2019

It would be helpful to have an injection point after the initial delayed registration.

We agree. We're tracking the enhancement here:

#1579

I think what we're proposing there will address your use case, so I'm closing this one for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.