title: Install package (library) if not installed
body: <p>I am using a couple of packages in R, but I am running the script in a machine that may or may not have some/all of the packages installed already.</p>
  
  <p>The packages are <code>zoo</code>, <code>quantmod</code>, <code>data.table</code>,..., and a bunch more.</p>
  
  <p>This is what I have tried:
  Is there any way of checking if each of these packages is installed, if not install it? I don't want R to waste time reinstalling any package that is already there.</p>
  
  <p>This is what I have tried:</p>
  
  <pre><code>pckg = c("zoo", "tseries", "quantmod", "MASS", "graphics", "plyr", "data.table", "gridExtra") 
  
   is.installed &lt;- function(mypkg){
      is.element(mypkg, installed.packages()[,1])
   } 
  
   for(i in 1:length(pckg)) {
      if (!is.installed(pckg[i])){
           install.packages(pckg[i])
       }
   }
  </code></pre>
  
  <p>Is there a better way of doing that?</p>
  
  <p>Also, I need to <strong>automatically set a mirror for the install</strong>.I have no idea how to do so.</p>
  
  <p>Thanks!</p>