title: Using data.table package inside my own package
body: <p>I am trying to use the data.table package inside my own package.  MWE is as follows:</p>
  
  <p>I create a function, test.fun, that simply creates a small data.table object, and then sums the "Val" column grouping by the "A" column.  The code is</p>
  
  <pre><code>test.fun&lt;-function ()
  {
      library(data.table)
      testdata&lt;-data.table(A=rep(seq(1,5), 5), Val=rnorm(25))
      setkey(testdata, A)
      res&lt;-testdata[,{list(Ct=length(Val),Total=sum(Val),Avg=mean(Val))},"A"]
      return(res)
  }
  </code></pre>
  
  <p>When I create this function in a regular R session, and then run the function, it works as expected.</p>
  
  <pre><code>&gt; res&lt;-test.fun()
  data.table 1.8.0  For help type: help("data.table")
  &gt; res
       A Ct      Total        Avg
  [1,] 1  5 -0.5326444 -0.1065289
  [2,] 2  5 -4.0832062 -0.8166412
  [3,] 3  5  0.9458251  0.1891650
  [4,] 4  5  2.0474791  0.4094958
  [5,] 5  5  2.3609443  0.4721889
  </code></pre>
  
  <p>When I put this function into a package, install the package, load the package, and then run the function, I get an error message.</p>
  
  <pre><code>&gt; library(testpackage)
  &gt; res&lt;-test.fun()
  data.table 1.8.0  For help type: help("data.table")
  Error in `[.data.frame`(x, i, j) : object 'Val' not found
  </code></pre>
  
  <p>Can anybody explain to me why this is happening and what I can do to fix it.  Any help is very much appreciated.</p>