R: Looping through list and passing values to API of UN Comtrade. + Programming a pause in the loop
<p>I want to pass the arguments of a list to a function calling the API of the UN Comtrade Data. Also, I need to add pauses after each query to the API to not exceed the restrictions of UN Comtrade. </p>

<p>Without the loop I call the API this way:</p>

<p><code>q276a &lt;- get.Comtrade(r="276", p="0", freq="M", ps="all", cc="842520", rg="2", fmt="csv")
df_q276a&lt;-as.data.frame(do.call(rbind, q1a)</code>.</p>

<p><code>r="276"</code> is specifying that I want the data from the Reporting Country with the code "276" which is Germany. To get data from all reporting countries I want to loop through a list with all the country codes. (I can't just specify "all" since the API restricts the value "all" for only one of the paramters r, p, ps)</p>

<p>What I've written so far is: </p>

<pre><code>for (i in ls_reporters){
  assign(paste("q", i,"a", sep = ""), get.Comtrade(r="i", p="0", freq="M", ps="all", cc="842520", rg="2", fmt="csv"))
}
</code></pre>

<p>This produces the error </p>

<blockquote>
  <p>Error in file(file, "rt") : cannot open the connection</p>
</blockquote>

<p>My code seems to pass <code>r="i"</code> to the API instead of cycling through the values in ls_reporters und passing these to the API, e.g. 4 then 18 and so on. However, if I type i I get "4" which is the first value of the ls_reporters. </p>

<p>As additional Question: How can i program a pause of a few seconds after each iteration to not exceed the 1 request per second restriction of UN Comtrade?</p>

<p>If there is a more elegant way programming this without loops I would be glad to learn it. I am pretty new to R. </p>

<p>The R code to query data through the public API can be found <a href="http://comtrade.un.org/data/Doc/api/ex/r" rel="nofollow">here</a>.
The documentation for the API can be found <a href="http://comtrade.un.org/data/doc/api/" rel="nofollow">here</a></p>