<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/a-python-like-walk-function-for-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://matloff.wordpress.com/2017/04/08/a-python-like-walk-function-for-r/"> Mad (Data) Scientist</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A <a target="_blank" rel="nofollow" href="http://www.pythonforbeginners.com/code-snippets-source-code/python-os-walk">really nice function </a>available in Python is <strong>walk()</strong>, which recursively descends a directory tree, calling a user-supplied function in each directory within the tree. It might be used, say, to count the number of files, or maybe to remove all small files and so on. I had students in my undergraduate class write such a function in R for homework, and thought it may be interesting to present it here.</p>
<p>Among other things, readers who are not familiar with recursive function calls will learn about those here. I must add that all readers, even those with such background, will find this post to be rather subtle and requiring extra patience, but I believe you will find it worthwhile.</p>
<p>Let’s start with an example, in which we count the number of files with a given name:</p>
<pre><code>
countinst &lt;- function(startdir,flname) {
   walk(startdir,checkname,
      list(flname=flname,tot=0))
}

checkname &lt;- function(drname,filelist,arg) {
   if (arg$flname %in% filelist) 
      arg$tot &lt;- arg$tot + 1
   arg
}
</code></pre>
<p>Say we try all this in a directory containing a subdirectory <strong>mydi</strong>r that consists of a file <strong>x,</strong> and two subdirectories, <strong>d1</strong> and <strong>d2</strong>. The latter in turn consists of another file named <strong>x</strong>. We then make the call <span><strong>countinst(‘mydir’,’x’)</strong>.  As can be seen above, that call will in turn make the call </span></p>
<pre><code>
walk('mydir',checkname,list(flname='x',tot=0)
</code></pre>
<p>The <strong>walk()</strong> function, which I will present below, will start in <strong>mydir</strong>, and will call the user-specified function <strong>checkname()</strong> at every directory it encounters in the tree rooted at <strong>mydir</strong>, in this case <strong>mydir</strong>, <strong>d1</strong> and <strong>d2</strong>.</p>
<p>At each such directory, <strong>walk()</strong> will pass to the user-specified function, in this case <strong>checkname()</strong>, first the current directory name, then a character vector reporting the names of all files (including directories) in that directory.  In <strong>mydir</strong>, for instance, this vector will be <strong>c(‘d1′,’d2′,’x’)</strong>.  In addition, <strong>walk()</strong> will pass to <strong>checkname() </strong>an argument, formally named <strong>arg</strong> above, which will serve as a vehicle for accumulating running totals, in this case the total number of files of the given name.</p>
<p>So, let’s look at <strong>walk()</strong> itself:</p>
<pre><code>
walk &lt;- function(currdir,f,arg) {
   # &quot;leave trail of bread crumbs&quot;
   savetop &lt;- getwd()
   setwd(currdir)
   fls &lt;- list.files()
   arg &lt;- f(currdir,fls,arg)
   # subdirectories of this directory
   dirs &lt;- list.dirs(recursive=FALSE)
   for (d in dirs) arg &lt;- walk(d,f,arg)
   setwd(savetop) # go back to calling directory
   arg  
}    

</code></pre>
<p>The comments are hopefully self-explanatory, but the key point is that within <strong>walk()</strong>, there is another call to <strong>walk()</strong>! This is recursion.</p>
<p>Note how <strong>arg</strong> accumulates, as needed in this application. If on the other hand we wanted, say, simply to remove all files named<strong> ‘x’</strong>, we would just put in a dummy variable for <strong>arg</strong>. And though we didn’t need drname here, in some applications it would be useful.</p>
<p>For compute-intensive tasks, recursion is not very efficient in R, but it can be quite handy in certain settings.</p>
<p>If you would like to conveniently try the above example, here is some test code:</p>
<pre><code>
test &lt;- function() {
   unlink('mydir',recursive=TRUE)
   dir.create('mydir')
   file.create('mydir/x')
   dir.create('mydir/d1')
   dir.create('mydir/d2')
   file.create('mydir/d2/x')
   print(countinst('mydir','x'))
}
</code></pre>
<p> </p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/matloff.wordpress.com/1783/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/matloff.wordpress.com/1783/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<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://matloff.wordpress.com/2017/04/08/a-python-like-walk-function-for-r/"> Mad (Data) Scientist</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/p4mV5uJX4Vc"></a></div>