title: Looping through files using dynamic name variable in R
body: <p>I have a large number of files to import which are all saved as zip files.</p>
  
  <p>From reading other posts it seems I need to pass the zip file name and then the name of the file I want to open.  Since I have a lot of them I thought I could loop through all the files and import them one by one.</p>
  
  <p>Is there a way to pass the name dynamically or is there an easier way to do this?</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>Temp_Data &lt;- NULL
  Master_Data &lt;- NULL
  
  
  file.names &lt;- c("f1.zip", "f2.zip", "f3.zip", "f4.zip", "f5.zip")
  
  for (i in 1:length(file.names)) {
      zipFile &lt;- file.names[i]
      dataFile &lt;- sub(".zip", ".csv", zipFile)
  
      Temp_Data &lt;- read.table(unz(zipFile, 
                              dataFile), sep = ",")
  
      Master_Data &lt;- rbind(Master_Data, Temp_Data)
  
  }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>In open.connection(file, "rt") :
  </code></pre>
  
  <p>I can import them manually using:</p>
  
  <pre><code>dt &lt;- read.table(unz("D:/f1.zip", "f1.csv"), sep = ",")
  </code></pre>
  
  <p>I can create the sting dynamically but it feels long winded - and doesn't work when I wrap it with read.table(unz(...)). It seems it can't find the file name and so throws an error</p>
  
  <pre><code>cat(paste(toString(shQuote(paste("D:/",zipFile, sep = ""))),",",
        toString(shQuote(dataFile)), sep = ""), "\n")
  </code></pre>
  
  <p>But if I then print this to the console I get:</p>
  
  <pre><code>"D:/f1.zip","f1.csv"
  </code></pre>
  
  <p>I can then paste this into `read.table(unz(....)) and it works so I feel like I am close</p>
  
  <p>I've tagged in data.table since this is what I almost always use so if it can be done with 'fread' that would be great.</p>
  
  <p>Any help is appreciated</p>