Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-Forge #4924] getDividends and getSplits to handle env argument properly #33

Closed
joshuaulrich opened this issue Jan 31, 2015 · 0 comments
Labels

Comments

@joshuaulrich
Copy link
Owner

joshuaulrich commented Jan 31, 2015

Submitted by: Stewart Wright
Assigned to: Nobody
R-Forge link

getDividends and getSplits should be able to update Symbol not in the parent environment, but they actually don't.

The 'svn diff' below fixes this by creating a temporary variable which gets updated and then reinserted into the appropriate environment.

I have to use the tmp.symbol variable as xtsAttributes only updates variables in the parent.frame.

Index: getDividends.R
===================================================================
--- getDividends.R  (revision 609)
+++ getDividends.R  (working copy)
@@ -2,8 +2,14 @@
 function(Symbol,from='1970-01-01',to=Sys.Date(),env=parent.frame(),src='yahoo',
          auto.assign=FALSE,auto.update=FALSE,verbose=FALSE,...) {

-  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),
@@ -27,10 +33,11 @@
   fr <- read.csv(tmp)
   unlink(tmp)
   fr <- xts(fr[,2],as.Date(fr[,1]))
-  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)
Index: getSplits.R
===================================================================
--- getSplits.R (revision 609)
+++ getSplits.R (working copy)
@@ -4,8 +4,13 @@

   # Function written by Joshua Ulrich, using
   # getSymbols.yahoo as a guide.
-  if(missing(env))
+  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),
@@ -39,10 +44,10 @@
     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)

Followups:

Date: 2013-09-26 05:26
Sender: Stewart Wright
Updated the patch.

Previous version did not handle case of just calling functions with a Symbol (i.e. 'F'): getDividends('F'). So now behavior is consistent.


Note: The code above reflects Stewart's updated patch.

joshuaulrich added a commit that referenced this issue Jun 20, 2021
By default, exists() searches the enclosing environments if the symbol
isn't found in the current environment. We don't want that. We only
want to check if it exists in the 'env' arg.

See #33.
joshuaulrich added a commit that referenced this issue Jun 20, 2021
The user probably doesn't want 'auto.assign = FALSE' (the default) if
they also set 'env' to a non-null value. Warn if they specify both
'auto.assign = FALSE' and 'env' (not null).

See #33.
@joshuaulrich joshuaulrich added this to the Release 0.4.20 milestone Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant