Skip to content

Commit

Permalink
Load dividends and splits into correct environment
Browse files Browse the repository at this point in the history
getDividends() and getSplits() always load to the parent environment,
even if the user specifies the 'env' argument.

Create a temporary variable that is updated and loaded into the
specified environment.

Thanks to Stewart Wright for the patch! Applied 6 years later.
Hopefully late is better than never...

Fixes #33.
  • Loading branch information
joshuaulrich committed Jun 20, 2021
1 parent 4bb1648 commit 94485da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 10 additions & 4 deletions R/getDividends.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ function(Symbol,from='1970-01-01',to=Sys.Date(),env=parent.frame(),src='yahoo',
auto.assign=FALSE,auto.update=FALSE,verbose=FALSE,split.adjust=TRUE,...,
curl.options=list()) {

if(missing(env))
tmp.symbol <- Symbol
if(missing(env)) {
env <- parent.frame(1)
} else {
if(exists(Symbol, envir = env)) {
tmp.symbol <- get(Symbol, envir = env)
}
}
if(is.null(env))
auto.assign <- FALSE
Symbol.name <- ifelse(!is.character(Symbol),
Expand Down Expand Up @@ -36,10 +42,10 @@ function(Symbol,from='1970-01-01',to=Sys.Date(),env=parent.frame(),src='yahoo',
}
}

if(is.xts(Symbol)) {
if(is.xts(tmp.symbol)) {
if(auto.update) {
xtsAttributes(Symbol) <- list(dividends=fr)
assign(Symbol.name,Symbol,envir=env)
xtsAttributes(tmp.symbol) <- list(dividends=fr)
assign(Symbol.name,tmp.symbol,envir=env)
}
} else if(auto.assign) {
assign(paste(Symbol.name,'div',sep='.'),fr,envir=env)
Expand Down
14 changes: 10 additions & 4 deletions R/getSplits.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ function(Symbol,from='1970-01-01',to=Sys.Date(),env=parent.frame(),src='yahoo',

# Function written by Joshua Ulrich, using
# getSymbols.yahoo as a guide.
if(missing(env))
tmp.symbol <- Symbol
if(missing(env)) {
env <- parent.frame(1)
} else {
if(exists(Symbol, envir = env)) {
tmp.symbol <- get(Symbol, envir = env)
}
}
if(is.null(env))
auto.assign <- FALSE
Symbol.name <- ifelse(!is.character(Symbol),
Expand Down Expand Up @@ -36,10 +42,10 @@ function(Symbol,from='1970-01-01',to=Sys.Date(),env=parent.frame(),src='yahoo',
colnames(fr) <- paste(Symbol.name,'spl',sep='.')
}

if(is.xts(Symbol)) {
if(is.xts(tmp.symbol)) {
if(auto.update) {
xtsAttributes(Symbol) <- list(splits=fr)
assign(Symbol.name,Symbol,envir=env)
xtsAttributes(tmp.symbol) <- list(splits=fr)
assign(Symbol.name,tmp.symbol,envir=env)
}
} else if(auto.assign) {
assign(paste(Symbol.name,'spl',sep='.'),fr,envir=env)
Expand Down

0 comments on commit 94485da

Please sign in to comment.