Skip to content

Commit

Permalink
Set Symbol value for symbols with no data
Browse files Browse the repository at this point in the history
Alphavantage returns NULL for all the columns of data for requests on
invalid symbols. We need to set the Symbol column to the value of the
symbol input in order to return a row of NA for the symbol in the
results, as we do for other getQuote() methods.

See #296.
  • Loading branch information
joshuaulrich committed Dec 8, 2020
1 parent f5b44fa commit 0493d4a
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions R/getQuote.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,29 +304,37 @@ getQuote.av <- function(Symbols, api.key, ...) {

# Function to process each quote response
quote2df <-
function(response, map)
function(response, map, symbol)
{
# Expected response structure
qres <- setNames(vector("list", nrow(map)), map[["av.names"]])

elem <- function(el)
elem <- function(el, isnum)
{
# process numeric columns
res <- NA_real_
if (!is.null(el)) {
haspct <- grepl("%", el, fixed = TRUE)
if (haspct) {
el <- sub("%", "", el, fixed = TRUE)
res <- as.numeric(el) / 100
if (isnum) {
# process numeric columns
haspct <- grepl("%", el, fixed = TRUE)
if (haspct) {
el <- sub("%", "", el, fixed = TRUE)
res <- as.numeric(el) / 100
} else {
res <- as.numeric(el)
}
} else {
res <- as.numeric(el)
res <- el
}
}
res
}
tmp <- modifyList(qres, response)
tonum <- map[["is.number"]]
tmp[tonum] <- lapply(tmp[tonum], elem)
tmp <- Map(elem, el = tmp, isnum = map[["is.number"]])

# populate Symbol column for symbols missing quotes
if (is.na(tmp[["01. symbol"]])) {
tmp[["01. symbol"]] <- symbol
}

data.frame(tmp, stringsAsFactors = FALSE)
}
Expand All @@ -348,7 +356,7 @@ getQuote.av <- function(Symbols, api.key, ...) {
call. = FALSE, immediate. = TRUE)
} else {
resp <- resp[[1]] # resp$`Global Quote`
qlist[[Symbol]] <- quote2df(resp, map)
qlist[[Symbol]] <- quote2df(resp, map, Symbol)
}
}

Expand Down

0 comments on commit 0493d4a

Please sign in to comment.