title: Identifying dependencies of R functions and scripts
body: <p>I am sifting through a package and scripts that utilize the package, and would like to identify external dependencies.  The goal is to modify scripts to specify <code>library(pkgName)</code> and to modify functions in the package to use <code>require(pkgName)</code>, so that these dependencies will be more obvious later.</p>
  
  <p>I am revising the code to account for each externally dependent package.  As an example, though it is by no means definitive, I am now finding it difficult to identify code that depends on <code>data.table</code>.  I could replace <code>data.table</code> with <code>Matrix</code>, <code>ggplot2</code>, <code>bigmemory</code>, <code>plyr</code>, or many other packages, so feel free to answer with examples based on other packages.</p>
  
  <p>This search isn't particularly easy.  The approaches I have tried so far include:</p>
  
  <ul>
  <li>Search the code for <code>library</code> and <code>require</code> statements</li>
  <li>Search for mentions of <code>data.table</code> (e.g. <code>library(data.table)</code>)</li>
  <li>Try running <code>codetools::checkUsage</code> to determine where there may be some issues.  For the scripts, my program inserts the script into a local function and applies <code>checkUsage</code> to that function.  Otherwise, I use <code>checkUsagePackage</code> for the package.</li>
  <li>Look for statements that are somewhat unique to <code>data.table</code>, such as <code>:=</code>.</li>
  <li>Look for where objects' classes may be identified via Hungarian notation, such as <code>DT</code></li>
  </ul>
  
  <p>The essence of my searching is to find:</p>
  
  <ul>
  <li>loading of <code>data.table</code>, </li>
  <li>objects with names that indicate they are <code>data.table</code> objects,</li>
  <li>methods that appear to be <code>data.table</code>-specific</li>
  </ul>
  
  <p>The only easy part of this seems to be finding where the package is loaded.  Unfortunately, not all functions may explicitly load or require the external package - these may assume it has already been loaded.  This is a bad practice, and I am trying to fix it.  However, searching for objects and methods seems to be challenging.</p>
  
  <p>This (<code>data.table</code>) is just one package, and one with what seems to be limited and somewhat unique usage.  Suppose I wanted to look for uses of ggplot functions, where the options are more extensive, and the text of the syntax is not as idiosyncratic (i.e. frequent usage of <code>+</code> is not idiosyncratic, while <code>:=</code> seems to be).</p>
  
  <p>I don't think that static analysis will give a perfect answer, e.g. one could pass an argument to a function, which specifies a package to be loaded.  Nonetheless: are there any core tools or packages that can improve on this brute force approach, either via static or dynamic analysis?</p>
  
  <p>For what it's worth, <code>tools::pkgDepends</code> only addresses dependencies at the package level, not the function or script level, which is the level I'm working at.</p>
  
  <hr>
  
  <p>Update 1: An example of a dynamic analysis tool that should work is one that reports which packages are loaded during code execution.  I don't know if such a capability exists in R, though - it would be like <code>Rprof</code> reporting the output of <code>search()</code> instead of the code stack.</p>