title: Loop for extracting variable from each document and placing in appropriate column
body: <p>My company documents summaries of policies/services for each client in a pdf formatted file. These files are combined into a large dataset each year. One row per client and columns are variables in the client's document. There are a couple thousand of these files and each one has approximately 20-30 variables each. I' want to automate this process by creating a data.frame with each row representing a client, and then pull the variables for each client from their pdf document. I'm able to create a list or data.frame of all the clients by the pdf filename in a directory but don't know how to create a loop that pulls each variable I need for each document. I currently have two different methods which I can't decide between, and also need help with a loop that grabs the variables I need for each client document. My code and links to two mock files are provided below. Any help would be appreciated!</p>
  
  <p>Files: <a href="https://drive.google.com/open?id=0B3UuqB6Ta76xYzVFWnJCc21ENkU" rel="nofollow noreferrer">Client 1</a> and <a href="https://drive.google.com/open?id=0B3UuqB6Ta76xNEp2TEx0anNQbkU" rel="nofollow noreferrer">Client 2</a></p>
  
  <p>Method 1: pdftools</p>
  
  <p>The benefit of the first method is it extracts the entire pdf into a vector, and each page into a separate element. This makes it easier for me to pull strings/variables. However, don't know how to loop it to pull the information from each client and appropriately place it in a column for each client.</p>
  
  <pre><code>library(pdftools)
  library(stringr)
  
  Files &lt;- list.files(path="...", pattern=".pdf")
  Files &lt;- Files %% mutate(FR =
    str_match(text, "\\$\\d+\\s\\Financial Reporting")) #Extract the first variable
  </code></pre>
  
  <p>Method 2: </p>
  
  <p>The benefit of this approach is it automatically creates a database for each of the client documents with file name as a row, and the each pdf in a variable. The downside is an entire pdf in a variable makes it more difficult to match and extract strings compared to having each page in its own element. I don't know how to write a loop that will extract variables for each client and place them in their respective column.</p>
  
  <pre><code>DF &lt;- readtext("directory pathway/*.pdf")
  DF &lt;- DF %&gt;% mutate(FR =
                      str_match(text, "\\$\\d+\\s\\Financial Reporting"))
  </code></pre>