Skip to content

Injection Point to Override RStudio Hooks #4498

Closed

Description

@coatless

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

Activity

  1. jmcphers commented on Mar 27, 2019

    @jmcphers
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions