title: .First function in R
body: <p>I don't understand the point of .First function in R. My reason is any code in .Rprofile will be sourced and executed when R starts up anyway.</p>
  
  <p>this</p>
  
  <pre><code>.First&lt;-function(){                                                                 
      library('devtools')                                     
  }
  </code></pre>
  
  <p>and this</p>
  
  <pre><code>library('devtools')
  </code></pre>
  
  <p>in .Rprofile have exactly the same effect. </p>
  
  <p>However, here is an example that shows .First can make a difference:</p>
  
  <p>example 1, you can see X11.options()$type correctly becomes Xlib as set in .Rprofile</p>
  
  <pre><code>&gt;&gt; cat .Rprofile
  .First &lt;- function() {
      library(devtools)
  }
  
  setHook(
      packageEvent("grDevices", "onLoad"),
      function(...) grDevices::X11.options(type="Xlib")
  )
  
  &gt;&gt; Rscript -e 'X11.options()$type'
  [1] "Xlib"
  </code></pre>
  
  <p>example 2, you can see X11.options()$type is still cairo, the setHook in .Rprofile didn't take effect</p>
  
  <pre><code>&gt;&gt; cat .Rprofile
  library(devtools)
  
  setHook(
      packageEvent("grDevices", "onLoad"),
      function(...) grDevices::X11.options(type="Xlib")
  )
  
  &gt;&gt; Rscript -e 'X11.options()$type'
  [1] "cairo"
  </code></pre>
  
  <ul>
  <li>in what case do I absolutely have to use .First function? </li>
  <li>why .First made a difference in the example above?</li>
  </ul>
  
  <p>Thanks!</p>