title: How to initialize R function during first run or whenever input changes
body: <p>I'm new to R and have some trouble of understanding so called "envirionments" and way to use them properly. What I miss a lot in R language are static variables (like in Java).</p>
  
  <p>I'm writing a program with couple of functions that will need to initialize during first run. To achieve this for each function I've created new environment which will be only accessed by this particular function (for example "f1" will be only accessed from inside "myfunction1").</p>
  
  <p>What I don't like about my solution is that there is some additional code outside of function body and it's not too readable. Is there any simpler way to achieve the same? And if yes then it would be nice if you could provide me with modified example to show me how it works. Thank you.</p>
  
  <pre><code>f1 &lt;- new.env()
  f1$initialized &lt;- FALSE
  f1$o &lt;- NULL
  f1$length &lt;- NULL
  f1$compute
  
  myfunction1 &lt;- function(x) {
    if(f1$initialized == FALSE){
      f1$initialized &lt;- TRUE
      f1$compute &lt;- 2*pi^2+3
    }
    if(is.null(f1$length) || f1$length!=length(x)){
      f1$length &lt;- length(x)
      if(f1$length==2) {f1$o&lt;-read.table("data_1.txt")}
      else {f1$o&lt;-read.table("data_2.txt")}
    }
    print("Lets print something!")
    return(f1$o * f1$compute * x + 1000)
  }
  </code></pre>