title: How to get the name of the calling function inside the called routine?
body: <p>Is there a "non-Internal" way to get the caller's name, as the function <code>stop</code> does?</p>
  
  <p>The idea is that I have a small function that checks the inputs and halts execution if some condition is not met. This function is called by several others, that use the same validation code. If the input is invalid, the caller's environment is dumped (so I can see arguments passed to the function), and execution halts.</p>
  
  <p>Simplified example:</p>
  
  <pre><code>check &lt;- function(x)
  {
      if(x&lt;0)
      {
          print(as.list(parent.frame()))
  
          evalq(stop("invalid input."), parent.frame())
      }
  }
  
  test &lt;- function(x, y)
  {
      check(x)
  }
  </code></pre>
  
  <p>I thought that evaluating the expression <code>quote(stop("blah"))</code> in the caller's environment would make it show the caller's name. However, the result is the following:</p>
  
  <pre><code>test(-1, 2)
  
  # $x
  # [1] -1
  # 
  # $y
  # [1] 2
  # 
  # Error in eval(substitute(expr), envir, enclos) : invalid input.
  </code></pre>
  
  <p>And this doesn't change if I use <code>parent.frame(n)</code> with <code>n&gt;1</code> in <code>evalq</code>.</p>
  
  <p>So here is the question, actually two questions: 1. Is there a way to get the name of the function that created an environment (assuming it was created as such)? 2. Why the workaround above fails?</p>
  
  <p>EDIT: I said the workaround above fails because I wanted the error message to appear as</p>
  
  <pre><code>Error in test(x, y) : invalid input.
  </code></pre>
  
  <p>as if the <code>stop</code> statement were a part of <code>test</code> body. So question 2 can be restated as: 2': Why didn't the evaluation of <code>stop("invalid input.")</code> capture the caller's name, considering it was evaluated in the environment of the caller?</p>