title: How to call an internal R C function from your own code
body: <p>I'd like to reuse R code from the <code>stats</code> package that simulates contingency tables to compute a simulated <code>p.value</code> for a chi-squared test.</p>
  
  <p>When looking at the <code>chisq.test</code> function source code, you can see the following :</p>
  
  <pre><code> if (simulate.p.value &amp;&amp; all(sr &gt; 0) &amp;&amp; all(sc &gt; 0)) {
          setMETH()
          tmp &lt;- .Call(C_chisq_sim, sr, sc, B, E)
          STATISTIC &lt;- sum(sort((x - E)^2/E, decreasing = TRUE))
          PARAMETER &lt;- NA
          PVAL &lt;- (1 + sum(tmp &gt;= almost.1 * STATISTIC))/(B + 
              1)
      }
  </code></pre>
  
  <p>The interesting line here is the <code>.Call</code> call :</p>
  
  <pre><code>tmp &lt;- .Call(C_chisq_sim, sr, sc, B, E)
  </code></pre>
  
  <p>What I'd like to do, if it is possible, is to use this <code>C_chisq_sim</code> function in my own code, but I can't manage to do it. If I try with :</p>
  
  <pre><code>tmp &lt;- .Call(C_chisq_sim, sr, sc, B, E, PACKAGE="stats")
  </code></pre>
  
  <p>I get a <code>C_chisq_sim object not found</code> error. And if I try with :</p>
  
  <pre><code>tmp &lt;- .Call("C_chisq_sim", sr,sc,B,E, PACKAGE="stats") 
  </code></pre>
  
  <p>I get an error saying that the entry point is not in the loading table.</p>
  
  <p>I'd like a solution that would be cross-platform, if possible.</p>