Skip to content

Commit

Permalink
Avoid warning if good symbol follows failed symbol
Browse files Browse the repository at this point in the history
The first attempt to download a valid symbol will unexpectedly fail
after an invalid symbol fails as expected.

I ran the code below after setting 'silent = FALSE' in the try()
calls, so I could see the error.

  getSymbols("IBM;dummy1;GE", env = (e <- new.env()), verbose = TRUE)
  ## downloading  IBM .....

  ## done.
  ## downloading  dummy1 .....

  ## Warning: dummy1 download failed; trying again.
  ## Warning: Unable to import “dummy1”.
  ## dummy1 download failed after two attempts. Error message:
  ## HTTP error 404.
  ## downloading  GE .....

  ## Error in open.connection(file, "rt") :
  ##   Handle is already in use elsewhere.
  ## Warning: GE download failed; trying again.

The error suggests we need to close the connection after a failed
download in order to release the handle. Then re-create the connection
for any further download attempts.

Thanks to @helgasoft for the report and patch.

Fixes #307.
  • Loading branch information
joshuaulrich committed Jun 21, 2020
1 parent 354e378 commit c9777da
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ function(Symbols,env,return.class='xts',index.class="Date",

yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix,
interval, "history", handle)
fr <- try(read.csv(curl::curl(yahoo.URL, handle = handle$ch), na.strings="null"),
silent = TRUE)
conn <- curl::curl(yahoo.URL, handle = handle$ch)
fr <- try(read.csv(conn, na.strings="null"), silent = TRUE)

if (inherits(fr, "try-error")) {
# warn user about the failure
Expand All @@ -346,10 +346,12 @@ function(Symbols,env,return.class='xts',index.class="Date",
# try again. must rebuild url with crumbs
yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix,
interval, "history", handle)
fr <- try(read.csv(curl::curl(yahoo.URL, handle = handle$ch), na.strings="null"),
silent = TRUE)
close(conn)
conn <- curl::curl(yahoo.URL, handle = handle$ch)
fr <- try(read.csv(conn, na.strings="null"), silent = TRUE)
# error if second attempt also failed
if (inherits(fr, "try-error")) {
close(conn)
stop(Symbols.name, " download failed after two attempts. Error",
" message:\n", attr(fr, "condition")$message, call. = FALSE)
}
Expand Down

0 comments on commit c9777da

Please sign in to comment.