title: Getting name of a function passed as an argument to a function
body: <h1>Background</h1>
  
  <p>Function is passed as an argument to a function. The problem pertains to:</p>
  
  <ul>
  <li>getting the name of that function as a string for convenient subsequent manipulation</li>
  <li>locating that function within the package from which is called</li>
  <li>understanding <strong><code>::</code></strong> and <strong><code>:::</code></strong> calls</li>
  </ul>
  
  <h1>Example</h1>
  
  <p>Function <code>fun_tst</code> executes function <code>FUN</code> on x:</p>
  
  <pre><code>fun_tst &lt;- function(x = 1:100, FUN = mean) {
      return(FUN(x))
  }
  </code></pre>
  
  <h2><code>mean</code></h2>
  
  <pre><code>fun_tst()
  # [1] 50.5
  </code></pre>
  
  <h2><code>sum</code></h2>
  
  <pre><code>fun_tst(x = 1:1e3, FUN = sum)
  # [1] 500500
  </code></pre>
  
  <h1>Problem</h1>
  
  <pre><code>fun_tst &lt;- function(x = 1:100, FUN = mean) {
      msg &lt;- paste("Executing function", FUN)
      print(msg)
      return(FUN(x))
  } 
  
  
  fun_tst(x = 1:1e3, FUN = sum)
  </code></pre>
  
  <blockquote>
    <p>Error in paste("Executing function", FUN) :    cannot coerce type
    'builtin' to vector of type 'character'</p>
  </blockquote>
  
  <hr>
  
  <h1>Attempts</h1>
  
  <h2>1)</h2>
  
  <p>Interestingly, <code>print</code> can handle <code>FUN</code> object but results return function body.</p>
  
  <pre><code>fun_tst &lt;- function(x = 1:100, FUN = mean) {
      print(FUN)
      return(FUN(x))
  } 
  
  
  fun_tst(x = 1:1e3, FUN = sum)
  </code></pre>
  
  <blockquote>
    <p>function (..., na.rm = FALSE)  .Primitive("sum") [1] 500500</p>
  </blockquote>
  
  <h2>2) <code>subsitute</code></h2>
  
  <pre><code>fun_tst &lt;- function(x = 1:100, FUN = mean) {
      fun_name &lt;- substitute(FUN)
      msg &lt;- paste("Executing function", fun_name, collapse = " ")
      print(msg)
      return(FUN(x))
  } 
  
  
  fun_tst(x = 1:1e3, FUN = sum)
  
  &gt;&gt; fun_tst(x = 1:1e3, FUN = sum)
  [1] "Executing function sum"
  [1] 500500
  </code></pre>
  
  <p>Almost there but it looks like a total mess when used with <strong><code>::</code></strong> as in:</p>
  
  <pre><code>&gt;&gt; fun_tst(x = 1:1e3, FUN = dplyr::glimpse)
  [1] "Executing function :: Executing function dplyr Executing function glimpse"
   int [1:1000] 1 2 3 4 5 6 7 8 9 10 ..
  </code></pre>
  
  <h1>Desired results</h1>
  
  <pre><code>fun_tst(x = 1:1e3, FUN = dplyr::glimpse)
  # Executing function glimpse from package dplyr
  int [1:1000] 1 2 3 4 5 6 7 8 9 10 ...
  
  fun_tst(x = 1:1e3, FUN = sum)
  # Executing function sum from package base
  </code></pre>