title: R for loop to loop through subdirectories to read SAS txt and Setup files and bind in a dataframe
body: <p>I have a directory (dir2 in the code below) with 200 subdirectory folders, each of which contains a <code>.txt</code> data file and a <code>Setup.sas</code> file. I need to write a for loop that uses the <code>asciiSetupReader</code> package to loop over each subdirectory, read the files therein using the <code>sas_ascii_reader</code> function and row-bind all the resulting read objects to one data frame. I know this must be relatively simple but am having difficulties.</p>
  <p>I have generated a dataframe that has two columns: one of the list of file names of the <code>.txt</code> files and another of the list of accompanying <code>Setup.sas</code> files.</p>
  <pre><code>list_file_txt &lt;- list.files(path = './dir1/dir2', 
                          pattern='*Data.txt', 
                          recursive=TRUE)
  
  list_file_sas &lt;- list.files(path = './dir1/dir2', 
                          pattern='*Setup.sas', 
                          recursive=TRUE)
  
  files &lt;- as.data.frame(cbind(list_file_txt,list_file_sas))
  
  files &lt;- files %&gt;% 
    mutate(directory = str_sub(list_file_txt,1,7), 
           directory = paste0('/dir1/dir2/',directory))
  </code></pre>
  <p>I have attempted:</p>
  <pre><code>for (i in 1:nrow(files)) { 
    setwd(files$directory)
    sas_ascii_reader(dataset_name = '*Data.txt',
                     sas_name = '*Setup.sas', 
                     real_names = FALSE)
    }
  </code></pre>
  <p>Results in error,</p>
  <pre><code>Error in setwd(files$directory) : cannot change working directory
  
  </code></pre>
  <p>which I understand indicates that R is not recognizing the character strings in the files$directory column as file paths to reference.</p>
  <p>I have also tried (as referenced at <a href="https://stackoverflow.com/questions/49831148/how-to-import-files-from-subdirectories-and-name-them-with-subdirectory-name-r">How to import files from subdirectories and name them with subdirectory name R</a>)</p>
  <pre><code>library(tidyverse)
  tbl &lt;-
    list.files(path = './dir1/dir2', 
               recursive=TRUE) %&gt;% 
    map_dfr(sas_ascii_reader, 
            dataset_name = '*Data.txt',
            sas_name = '*Setup.sas', 
            .id = &quot;filepath&quot;)
  </code></pre>
  <p>but get</p>
  <pre><code>Error in .f(.x[[i]], ...) : is.logical(value_label_fix) is not TRUE
  
  </code></pre>
  <p>which I don't understand at all.</p>
  <p>Any help would be appreciated. Thanks all.</p>