Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First : do not ever in your life use that continue construct again. It's of no use. <code>tryCatch()</code> <em>will</em> continue if you defined a handler for an error or a warning. It will use that one instead of the "default" <code>error=function(e) stop(e)</code>. This one will stop your function. If you define a handler (either <code>warning=</code> or <code>error=</code> ), your script will not be stopped, so that continue is not necessary. </p> <p>This said : The correct use of tryCatch in this case would be :</p> <pre><code>for(i in 1:nrow(symbol)){ tryCatch(prices &lt;- getYahooData(symbol$symbol[i], from, to, freq="daily", type="price"), error = function(e){}) } </code></pre> <p>or, if you use it in a script and want to go to the next loop when an error occurs, you can simply use :</p> <pre><code>for(i in 1:nrow(symbol)){ prices &lt;- try(getYahooData(symbol$symbol[i], from, to, freq="daily", type="price"), silent=TRUE) if(inherits(prices,"try-error")) { next } # only true if an error occurs ... # rest of calculations } </code></pre> <p>If you would have used this way of tryCatch or try, you wouldn't have had the problems you report here. </p> <p>Now I could reproduce your case, if I use nonexistent symbols. Your erroneous use of <code>tryCatch()</code> function is causing you trouble. <code>read.table</code> returns an error (<code>Error in file(file, "rt") : cannot open the connection</code>). This is an <strong>error</strong>, not a warning. You get an additional warning saying that a 404 File not found was returned. </p> <p>When a warning is issued together with an error, the handler function for the warning is dealt with first. That's because a warning has to be thrown before the function can be stopped. So it won't handle the error you get, <s>which means that the <code>on.exit(close(file))</code> in <code>read.table()</code> will not to be called. Hence, the connection is not closed correctly and still considered open, although it can't be found anymore by R (showAllConnections() shows nothing).</s> As the error is not dealt with, something goes wrong in the registration of the connections. As the connection couldn't be opened, the <code>on.exit(close(...))</code> will have no effect. <code>showConnections()</code> doesn't show the connection, but somehow R still thinks it's there. Hence, all hell breaks loose and you crash your R.</p> <p><em>thanks for the corrections to @Tommy</em></p> <p>A simple code example to illustrate this :</p> <pre><code>myfun &lt;- function(x){ if(x&gt;1) warning("aWarning") stop("aStop") x } tryCatch(myfun(0.5), warning=function(w)print("warning"), error=function(e) print("stop")) [1] "stop" tryCatch(myfun(1.5), warning=function(w)print("warning"), error=function(e) print("stop")) [1] "warning" </code></pre> <p>In summary :</p> <ul> <li>check the symbols you use. They're probably wrong.</li> <li>do not ever again use a warning handler if you expect errors.</li> </ul> <p>And as an extra: your loop will only return the result of the last call, as you overwrite <code>prices</code> every time you go through the loop, in case you would have used correct symbols.</p> <p>Edit : in case you want to continue the action </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload