<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/the-mysterious-ellipsis-tutorial/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.r-exercises.com/2018/10/22/the-mysterious-ellipsis-tutorial/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2018/10/ellipsis.png?ssl=1" target="_blank"><img src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2018/10/ellipsis.png?resize=200%2C73&ssl=1" alt="" width="200" class="wp-image-11939" data-recalc-dims="1" height="73"></a>If you have any basic experience with R, you probably noticed that R uses three dots ellipsis (…) to allow functions to take arguments that weren’t pre-defined or hard-coded when the function was built. Even though R beginners are usually aware of this behavior, especially due to some common functions that implement it (for example, <code>paste()</code>), they are often not using it enough in their own functions. In other cases, the ellipsis is just not used properly or not fully taken advantage of. In this tutorial we will go through some common mistakes in using the ellipsis feature, and some interesting options to fully utilize it and the flexibility that it offers.</p>
<p><strong>Choose lists over vectors</strong><br>
The most common mistake is trying to assign the ellipsis content to a vector rather than a list. Well, of course it’s not so much of a mistake if we’re expecting only a single data type from the ellipsis arguments, but this is often not the case and assigning the arguments to a vector rather than a list might cause problems when there’s a variety of data types.</p>
<p>So make sure you’re always unpacking the ellipsis content using the <code>list()</code> function rather than the <code>c()</code> function. As an example, try running this piece of code with both options:</p>
<p><code><br>
my_ellipsis_function &lt;- function(...) {<br>
args &lt;- list(...) # good<br>
# args &lt;- c(...) # bad<br>
length(args)<br>
}</code></p>
<p>my_ellipsis_function(“Hello World”, mtcars)</p>
<p><strong>Combine the ellipsis with other arguments</strong><br>
Some tend to think that it’s not possible to use the ellipsis with other regular arguments. This is not the case, and the ellipsis-arguments shouldn’t be the only ones in your function. You can combine them with as many regular arguments as you wish.</p>
<p><code><br>
my_ellipsis_function &lt;- function(x, ...) {<br>
print(paste(&quot;Class of regular-argument:&quot;, class(x)))<br>
print(paste(&quot;Number of ellipsis-arguments:&quot;, length(list(...))))<br>
}</code></p>
<p>my_ellipsis_function(x = “Hello World”, mtcars, c(1:10), list(“Abc”, 123))</p>
<p><strong>Don’t forget the names</strong><br>
In fact, the values of the arguments themselves are not the only information that is passed through the ellipsis-arguments. The names of the arguments (if specified) can also be used. For example:</p>
<p><code><br>
my_ellipsis_function &lt;- function(...) {<br>
names(list(...))<br>
}</code></p>
<p>my_ellipsis_function(some_number = 123, some_string = “abc”, some_missing_value = NA)</p>
<p>Lastly, somewhat of an advanced procedure might be unpacking the ellipsis-arguments into local function variables (or even global). There are all kind of scenarios where it might be needed (for global variables assignment it might be more intuitive). One example for a need in local variables, is where a certain function takes a certain regular-argument, that is dependent on a varying set of other variables. A use of the function glue::glue() within another function is a good example for that. The following code demonstrates how simple it is to perform this “unpacking”:</p>
<p><code><br>
my_ellipsis_function &lt;- function(...) {<br>
args &lt;- list(...)</code></p>
<p>for(i in 1:length(args)) {<br>
assign(x = names(args)[i], value = args[[i]])<br>
}</p>
<p>ls() # show the available variables</p>
<p># some other code and operations<br>
# that use the ellipsis-arguments as “native” variables…<br>
}</p>
<p>my_ellipsis_function(some_number = 123, some_string = “abc”)</p>
<p>So whether you’re an R beginners or not, don’t forget to utilize this convenient feature when needed, and use it wisely.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/10/21/mathematical-expressions-in-r-plots-tutorial/" title="Mathematical Expressions in R Plots: Tutorial" target="_blank">Mathematical Expressions in R Plots: Tutorial </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/11/29/spatial-data-analysis-introduction-to-raster-processing-part-1/" title="Spatial Data Analysis: Introduction to Raster Processing (Part 1)" target="_blank">Spatial Data Analysis: Introduction to Raster Processing (Part 1) </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/07/13/parallel-computing-exercises-foreach-and-doparallel-part-2/" title="Parallel Computing Exercises: Foreach and DoParallel (Part-2)" target="_blank">Parallel Computing Exercises: Foreach and DoParallel (Part-2) </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/custom-exercises/" target="_blank">Become a Top R Programmer Fast with our Individual Coaching Program</a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>4000) R exercises</a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.r-exercises.com/2018/10/22/the-mysterious-ellipsis-tutorial/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/OxdIDE-_cZQ" height="1"></a></div>