Home > Custom Function > A shortcut function for install.packages() and library()

A shortcut function for install.packages() and library()

I enjoy trying difference kind of R packages. Since I have more than 1 computers (1 at home, 1 at office and a laptop)
it is troublesome to check whether I have installed some new packages for each computer. Therefore i wrote a function to load and install packages at once. If the package does not exist, then the it will be downloaded from CRAN and be loaded it.

packages<-function(x, repos="http://cran.r-project.org", ...){
   x <- deparse(substitute(x))
   if (!require(x,character.only=TRUE)){
      install.packages(pkgs=x, repos=repos, ...)

Thanks richierocks for the suggestion of using deparse(substitute(x)) in the code.


Categories: Custom Function
  1. September 12, 2011 at 16:28

    Nice idea to combine install.packages and require. Couple of things about your code though.

    The standard way of retrieving the name of a variable in the parent environment is


    as.character does odd things for more complicated variable names. Compare

    f1 <- function(x) as.character(match.call()[[2]])
    f2 <- function(x) deparse(substitute(x))

    x <- 1:3

    Secondly, install.packages takes many arguments that it would be useful to be able to set. Use the ellipsis argument to achieve this.

    packages <- function(x, …){
    if(!is.character(x)) x <- deparse(substitute(x))
    if (!require(x, character.only = TRUE)){
    install.packages(x, …)
    require(x, character.only = TRUE)

  2. September 12, 2011 at 16:39

    Thanks for information!! Can I put your updated code into the main text of my post?

  3. eoecontributor
    September 14, 2011 at 09:37

    Have you tested that code? You can’t both test whether or not x is a character and use substitute – try running packages(mgcv).

    • September 14, 2011 at 13:48

      Good spot. No I hadn’t tested it.

      The problem is that when you pass a package name in unquoted, is.character gets confused and claims that the variable you passed in doesn’t exist.

      A slightly nasty workaround that I have checked.

      packages <- function(x, …){
      xname <- deparse(substitute(x))
      if(grepl('"', xname)) #input was a character vector anyway
      xname <- x
      if (!require(xname, character.only = TRUE)){
      install.packages(xname, …)
      require(xname, character.only = TRUE)

      The alternative if adding an argument that the user provides to say whether or not the first input was a character string or not. (This is what library does.)

  4. September 14, 2011 at 13:16

    Updated. Thank you!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: