Skip to content

Instantly share code, notes, and snippets.

@stevenworthington
Created July 25, 2012 19:44
Show Gist options
  • Save stevenworthington/3178163 to your computer and use it in GitHub Desktop.
Save stevenworthington/3178163 to your computer and use it in GitHub Desktop.
Install and load multiple R packages at once
# ipak function: install and load multiple R packages.
# check to see if packages are installed. Install them if they are not, then load them into the R session.
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
# usage
packages <- c("ggplot2", "plyr", "reshape2", "RColorBrewer", "scales", "grid")
ipak(packages)
@rossie123
Copy link

Hello stevenworthington,
I am not used to R, i am unix administrator. Could you please help to execute r packages in one shot. ( very Urgent)
As i dont have internet to the server, so cran not possble. I download the files locally and perform by this way in R console,

install.packages("/tmp/R_binaries_gz/Rsolnp_1.16.tar.gz", repos=NULL, type="source")

i have to change the package name and execute it manually for the rest of the package.

Thanks in Advance :)

@danpritts
Copy link

Using R 3.2.3 on linux. The if (length(new.pkg)) test is always returning true, so the packages are all being reinstalled even when not necessary.

@Espanta
Copy link

Espanta commented Apr 23, 2016

You can check and use lubripack package which is an s3 package on github. It is a bit easier and robust to use. github/espanta/lubripack -> here

@MatthewSchumwinger
Copy link

MatthewSchumwinger commented May 5, 2016

Just what I need after upgrading to latest version of R. Good stuff. Thank you.

@brentthorne
Copy link

Thanks for this ! I honestly use this in all my R!

@viditjain1
Copy link

Thanks a lot!

@SpencerVaradi
Copy link

Thanks! Huge help

@swapnilawasthi
Copy link

Thanks!!

Copy link

ghost commented Jun 13, 2017

Awesome Thanks!!!

@ecweidner
Copy link

Thanks!

Copy link

ghost commented Jul 14, 2017

Thanks, I've changed it a little and added top 100 packages. This was useful in a new fresh R installation I had to do. You might have failing packages depending on your system features or installed libraries. You might have to run it several times to install every package. Best

ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
packages <- c("ggplot2", "plyr", "reshape2", "RColorBrewer", "scales", "grid", c("plyr","digest","ggplot2","colorspace","stringr","RColorBrewer","reshape2","zoo","proto","scales","car","dichromat","gtable","munsell","labeling","Hmisc","rJava","mvtnorm","bitops","rgl","foreign","XML","lattice","e1071","gtools","sp","gdata","Rcpp","MASS","Matrix","lmtest","survival","caTools","multcomp","RCurl","knitr","xtable","xts","rpart","evaluate","RODBC","tseries","DBI","nlme","lme4","reshape","sandwich","leaps","gplots","abind","randomForest","Rcmdr","coda","maps","igraph","formatR","maptools","RSQLite","psych","KernSmooth","rgdal","RcppArmadillo","effects","sem","vcd","XLConnect","markdown","timeSeries","timeDate","RJSONIO","cluster","scatterplot3d","nnet","fBasics","forecast","quantreg","foreach","chron","plotrix","matrixcalc","aplpack","strucchange","iterators","mgcv","kernlab","SparseM","tree","robustbase","vegan","devtools","latticeExtra","modeltools","xlsx","slam","TTR","quantmod","relimp","akima","memoise"))
ipak(packages)

@Bosmansc
Copy link

thanks a lot!

@greatschism
Copy link

Thanks

@buddy1987
Copy link

Thanks

@Bharathreddy05
Copy link

I am new to R.
We have a setup like, R (R is on Linux) is connected to SAP HANA. We have a request to install R-packages.
Packages: rJava,RODBC,RJDBC,DBI,monmlp,AMORE,pmml,XML,arules,caret,reshape,plyr,foreach,iterators,adabag,randomForest,ggplot2

Can some one please let us know, How to install them.
As far i know, To install R- packages - i need to download R-package and install them by using command : Ex:
install.packages("/tmp/R_binaries_gz/Rsolnp_1.16.tar.gz", repos=NULL, type="source")

Here are my question now:

  1. How to check package is installed or not in Linux.
  2. How to check list of packages installed in R-server - Linux.
  3. Where to check error logs.
  4. How to uninstall the R- package in Linux.
  5. Is this package saved in to File syste level or Database level. In our case Database is HANA.

Could some one be kind and help me here.

@brentthorne
Copy link

brentthorne commented Jun 28, 2018

I've been using this for a few years now and just though that it might be cool if there could be a version of this that uses install_github or any of the devtools::install_ for packages as I find myself using this most often for my work.

I will try and figure out how to do that but I don't have much time to dedicate to it which is why I am tossing the idea out here for anyone else to tackle if they think they can do it!

Agian, thank you @stevenworthington for speeding up my workflow with this function 😄

Cheers!

EDIT:

Well I procrastinated at work and made a version of this that does this now found here. I haven't tested it for options other than install_method = "github" and install_method = "base" so feel free to check it out and change it as needed!

@AndreAlz
Copy link

Thanks bro!

@spydmobile
Copy link

Thanks for this 👍

@khaors
Copy link

khaors commented Mar 24, 2019

Just Awesome!!!! Thanks a lot

@JonathonMifsud
Copy link

Thank you for this!

@igorfarah
Copy link

Awesome!!

@sharonsunpeng
Copy link

Very useful function. Thanks for sharing this!

@timathomas
Copy link

I love this function!! Made an edit to install and/or load GitHub packages

ipak_gh <- function(pkg){
    new.pkg <- pkg[!(sub('.*/', '', pkg) %in% installed.packages()[, "Package"])]
    if (length(new.pkg)) 
        remotes::install_github(new.pkg, dependencies = TRUE)
    sapply(sub('.*/', '', pkg), require, character.only = TRUE)
}

# Example
ipak_gh(c("lmullen/gender", "mtennekes/tmap", "jalvesaq/colorout", "timathomas/neighborhood", "arthurgailes/rsegregation"))

@albertjimrod
Copy link

Another person happy thanks to you.
Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment