Search code examples
rautocompletepackagerstudior-infer

How to tell RStudio to autocomplete my function's arguments with package names?


According to RStudio:

In addition, certain functions, such as library() and require(), expect package names for completions. RStudio automatically infers whether a particular function expects a package name and provides those names as completion...

My question is: how? I'm writing a custom function that takes package names as arguments, yet RStudio's only completing the arguments with object & function names, and I can't tell what it is about the library() and require() code that RStudio is picking up on.

My function is:

unpack <- function(...,
                   lib   = NULL,
                   repos = getOption("repos")) {
  pkgs <- sapply(match.call(expand.dots = TRUE)[-1], as.character)
  new.pkgs <-
    pkgs[!(
      pkgs %in% installed.packages(lib.loc = lib)[, "Package"]
    )]
  if (length(new.pkgs))
    install.packages(new.pkgs,
                     lib   = lib,
                     repos = repos)
  sapply(pkgs, require,
         lib.loc = lib,
         character.only = TRUE)
}

Solution

  • As @hrbrmstr pointed out, there's both Java and R code that specifically name the four functions that autocomplete with package names, so the solution is to either mask one of those and cross your fingers, or add your function's name to those lists in both source files (or maybe just the R, I wonder).