<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/install-and-load-multiple-r-packages/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.listendata.com/2018/12/install-load-multiple-r-packages.html"> ListenData</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div dir="ltr">
<div>In enterprise environment, we generally need to automate the process of installing multiple R packages so that user does not have to install them separately before submitting your program.</div>
<div></div>
<div><span>The function below performs the following operations –</span></div>
<div></div>
<ol>
<li>First it finds all the already installed R packages</li>
<li>Check packages which we want to install are already installed or not.</li>
<li>If package is already installed, it <b>does</b> <b>not install</b> it again.</li>
<li>If package is missing (not installed), it installs the package.</li>
<li>Loop through steps 2, 3 and 4 for multiple packages we want to install</li>
<li>Load all the packages (both already available and new ones).</li>
</ol>
<p></p>
<blockquote><p>Install_And_Load &lt;-function(packages) {<br>  k &lt;-packages[!(packages %in% installed.packages()[,”Package”])];<br>  if(length(k))<br>  {install.packages(k, repos=’https://cran.rstudio.com/’);}</p>
<p>  for(package_name in packages)<br>  {library(package_name,character.only=TRUE, quietly = TRUE);}<br>}<br>Install_And_Load(c(“fuzzyjoin”, “quanteda”, “stringdist”, “stringr”, “stringi”))</p></blockquote>
<p><b><span>Explanation</span></b></p>
<p>1. <b>installed.packages()</b> returns details of all the already installed packages. <b>installed.packages()[,”Package”]</b> returns names of these packages.</p>
<p><b>To see version of the packages, submit the following command</b></p>
<blockquote><p>installed.packages()[,c(“Package”,”Version”)]</p></blockquote>
<p>2.  You can use any of the following repositories (URL of a CRAN mirror). You can experiment with these 3 repositories if one of them is blocked in your company due to firewall restriction.</p>
<blockquote><p>https://cloud.r-project.org<br>https://cran.rstudio.com<br>http://www.stats.ox.ac.uk/pub/RWin</p></blockquote>
<p>3. <b>quietly = TRUE </b>tells R not to print errors/warnings if package attaching (loading) fails.</p>
<p><b><span>How to check version of R while installation</span></b></p>
<p>In the program below, the package <b>RDCOMClient </b>refers repository – <b>http://www.omegahat.net/R</b> if R version is greater than or equal to 3.5. Else refers the repository <b>http://www.stats.ox.ac.uk/pub/RWin</b></p>
<blockquote><p>if (length(“RDCOMClient”[!(“RDCOMClient” %in% installed.packages()[,”Package”])])) {<br>  if (as.numeric(R.Version()$minor)>= 5)<br>    install.packages(“RDCOMClient”, repos = “http://www.omegahat.net/R”)<br>  else<br>    install.packages(“RDCOMClient”, repos = “http://www.stats.ox.ac.uk/pub/RWin”)<br>}<br>library(“RDCOMClient”)</p></blockquote>
</div>
<div><span> About Author:</span> 
<p>Deepanshu founded ListenData with a simple objective – Make analytics easy to understand and follow. He has over 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains.</p>
<p>Let's Get Connected: <a rel="nofollow" href="https://www.linkedin.com/in/deepanshubhalla" target="_blank">LinkedIn</a></p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.listendata.com/2018/12/install-load-multiple-r-packages.html"> ListenData</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/KFbEQTx_3qg" height="1"></a></div>