Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a practical example of passing arguments using the <code>...</code> object and <code>*apply</code>. It's slick, and this seemed like an easy example to explain the use. An important point to remember is when you define an argument as <code>...</code> all calls to that function must have named arguments. (so R understands what you're trying to put where). For example, I could have called <code>times &lt;- fperform(longfunction, 10, noise = 5000)</code> but leaving off <code>noise =</code> would have given me an error because it's being passed through <code>...</code> My personal style is to name all of the arguments if a <code>...</code> is used just to be safe. </p> <p>You can see that the argument <code>noise</code> is being defined in the call to <code>fperform(FUN = longfunction, ntimes = 10, noise = 5000)</code> but isn't being used for another 2 levels with the call to <code>diff &lt;- rbind(c(x, runtime(FUN, ...)))</code> and ultimately <code>fun &lt;- FUN(...)</code></p> <pre class="lang-r prettyprint-override"><code># Made this to take up time longfunction &lt;- function(noise = 2500, ...) { lapply(seq(noise), function(x) { z &lt;- noise * runif(x) }) } # Takes a function and clocks the runtime runtime &lt;- function(FUN, display = TRUE, ...) { before &lt;- Sys.time() fun &lt;- FUN(...) after &lt;- Sys.time() if (isTRUE(display)) { print(after-before) } else { after-before } } # Vectorizes runtime() to allow for multiple tests fperform &lt;- function(FUN, ntimes = 10, ...) { out &lt;- sapply(seq(ntimes), function(x) { diff &lt;- rbind(c(x, runtime(FUN, ...))) }) } times &lt;- fperform(FUN = longfunction, ntimes = 10, noise = 5000) avgtime &lt;- mean(times[2,]) print(paste("Average Time difference of ", avgtime, " secs", sep="")) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload