title: In R, how to make the variables inside a function available to the lower level function inside this function?(with, attach, environment)
body: <p><strong>Update 2</strong>
  @G. Grothendieck posted two approaches. The second one is changing the function environment inside a function. This solves my problem of too many coding replicates. I am not sure if this is a good method to pass through the CRAN check when making my scripts into a package. I will update again when I have some conclusions.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I am trying to pass a lot of input argument variables to <code>f2</code> and do not want to index every variable inside the function as <code>env$c, env$d, env$calls</code>, that is why I tried to use <code>with</code> in <code>f5</code> and <code>f6</code>(a modified <code>f2</code>). However, <code>assign</code> does not work with <code>with</code> inside the <code>{}</code>, moving <code>assign</code> outside <code>with</code> will do the job but in my real case I have a few <code>assign</code>s inside the <code>with</code> expressions which I do not know how to move them out of the <code>with</code> function easily.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>## In the &lt;environment: R_GlobalEnv&gt;
  a &lt;- 1
  b &lt;- 2
  f1 &lt;- function(){
      c &lt;- 3
  d &lt;- 4
  f2 &lt;- function(P){
      assign("calls", calls+1, inherits=TRUE)
      print(calls)
      return(P+c+d)
   }
  calls &lt;- 0
  v &lt;- vector()
  for(i in 1:10){
      v[i] &lt;- f2(P=0)
      c &lt;- c+1
      d &lt;- d+1
    }
   return(v)
  }
  f1()
  </code></pre>
  
  <p>Function <code>f2</code> is inside <code>f1</code>, when <code>f2</code> is called, it looks for variables <code>calls,c,d</code> in the environment <code>environment(f1)</code>. This is what I wanted.</p>
  
  <p>However, when I want to use <code>f2</code> also in the other functions, I will define this function in the Global environment instead, call it <code>f4</code>.</p>
  
  <pre><code>f4 &lt;- function(P){
    assign("calls", calls+1, inherits=TRUE)
    print(calls)
    return(P+c+d)
  }
  </code></pre>
  
  <p>This won't work, because it will look for <code>calls,c,d</code> in the Global environment instead of inside a function where the function is called. For example:</p>
  
  <pre><code>f3 &lt;- function(){
    c &lt;- 3
    d &lt;- 4
    calls &lt;- 0
    v &lt;- vector()
    for(i in 1:10){
      v[i] &lt;- f4(P=0) ## or replace here with f5(P=0)
      c &lt;- c+1
      d &lt;- d+1
    }
    return(v)
  }
  f3()
  </code></pre>
  
  <p>The safe way should be define <code>calls,c,d</code> in the input arguments of <code>f4</code> and then pass these parameters into <code>f4</code>. However, in my case, there are too many variables to be passed into this function <code>f4</code> and it would be better that I can pass it as an environment and tell <code>f4</code> do not look in the Global environment(<code>environment(f4)</code>), only look inside the <code>environment</code> when <code>f3</code> is called. </p>
  
  <p>The way I solve it now is to use the environment as a list and use the <code>with</code> function. </p>
  
  <pre><code>f5 &lt;- function(P,liste){
    with(liste,{
       assign("calls", calls+1, inherits=TRUE)
       print(calls)
       return(P+c+d)
       }
    )
  }
  f3 &lt;- function(){
    c &lt;- 3
    d &lt;- 4
    calls &lt;- 0
    v &lt;- vector()
    for(i in 1:10){
      v[i] &lt;- f5(P=0,as.list(environment())) ## or replace here with f5(P=0)
      c &lt;- c+1
      d &lt;- d+1
    }
    return(v)
  }
  f3()
  </code></pre>
  
  <p>However, now <code>assign("calls", calls+1, inherits=TRUE)</code> does not work as it should be since <code>assign</code> does not modify the original object. The variable <code>calls</code> is connected to an optimization function where the objective function is <code>f5</code>. That is the reason I use <code>assign</code> instead of passing <code>calls</code> as an input arguments. Using <code>attach</code> is also not clear to me. Here is my way to correct the <code>assign</code> issue:</p>
  
  <pre><code>f7 &lt;- function(P,calls,liste){
    ##calls &lt;&lt;- calls+1
    ##browser()
    assign("calls", calls+1, inherits=TRUE,envir = sys.frame(-1))
    print(calls)
    with(liste,{
      print(paste('with the listed envrionment, calls=',calls))
      return(P+c+d)
    }
    )
  }
  ########
  ##################
  f8 &lt;- function(){
    c &lt;- 3
    d &lt;- 4
    calls &lt;- 0
    v &lt;- vector()
    for(i in 1:10){
      ##browser()
      ##v[i] &lt;- f4(P=0) ## or replace here with f5(P=0)
      v[i] &lt;- f7(P=0,calls,liste=as.list(environment()))
      c &lt;- c+1
      d &lt;- d+1
    }
    f7(P=0,calls,liste=as.list(environment()))
    print(paste('final call number',calls))
    return(v)
  }
  f8()
  </code></pre>
  
  <p>I am not sure how this should be done in R.  Am I on the right direction, especially when passing through the CRAN check? Anyone has some hints on this?</p>