[Blotter-commits] r957 - in pkg/FinancialInstrument: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Mar 4 02:35:01 CET 2012


Author: gsee
Date: 2012-03-04 02:35:01 +0100 (Sun, 04 Mar 2012)
New Revision: 957

Modified:
   pkg/FinancialInstrument/DESCRIPTION
   pkg/FinancialInstrument/R/saveInstruments.R
   pkg/FinancialInstrument/man/saveInstruments.Rd
Log:
 - loadInstruments can be called with an environment or a list instead of the name of 
    a file.

Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION	2012-03-03 19:19:06 UTC (rev 956)
+++ pkg/FinancialInstrument/DESCRIPTION	2012-03-04 01:35:01 UTC (rev 957)
@@ -11,7 +11,7 @@
     meta-data and relationships. Provides support for
     multi-asset class and multi-currency portfolios. Still
     in heavy development.
-Version: 0.12.4
+Version: 0.12.5
 URL: https://r-forge.r-project.org/projects/blotter/
 Date: $Date$
 Depends:

Modified: pkg/FinancialInstrument/R/saveInstruments.R
===================================================================
--- pkg/FinancialInstrument/R/saveInstruments.R	2012-03-03 19:19:06 UTC (rev 956)
+++ pkg/FinancialInstrument/R/saveInstruments.R	2012-03-04 01:35:01 UTC (rev 957)
@@ -33,15 +33,15 @@
 #' load instruments back into the .instrument environment.
 #' 
 #' @aliases saveInstruments loadInstruments
-#' @param file_name What to name the file (name of file that holds a
-#' .instrument enviroment) Does not include file extension.
+#' @param file_name name of file. e.g. \dQuote{MyInstruments.RData}.  
+#' As an experimental feature, a \code{list} or \code{environment} can be passed 
+#' to \code{file_name}.
 #' @param dir Directory of file (defaults to current working directory. ie. "")
 #' @return Called for side-effect
 #' @author Garrett See
 #' @seealso save, load load.instrument define_stocks, define_futures,
 #' define_options (option_series.yahoo)
 #' @examples
-#' 
 #' \dontrun{
 #' stock("SPY", currency("USD"), 1)
 #' tmpdir <- tempdir()
@@ -72,12 +72,12 @@
         cat("#auto-generated by FinancialInstrument:::saveInstruments\n\n",
             "require(FinancialInstrument)\n\n", file=file.name)
         for (s in ls_instruments()) {
+            sink(file.name, append=TRUE)
             cat('assign("', s, '", pos=FinancialInstrument:::.instrument, ', 
-                'value=\n', file=file.name, sep="", append=TRUE)
-            sink(file.name, append=TRUE)
+                'value=\n', sep="", append=TRUE)
             dput(getInstrument(s))
+            cat(")\n\n", append=TRUE)
             sink()
-            cat(")\n\n", file=file.name, append=TRUE)
             #system(paste("cat", file.name)) #for debugging    
         }
     } else save(.instrument, file = file.name)	
@@ -88,7 +88,15 @@
 #' @rdname saveInstruments
 loadInstruments <-function(file_name="MyInstruments", dir="") {
     require("utils")
-	if (!is.null(dir) && !dir == "" && substr(dir,nchar(dir),nchar(dir)) != "/")
+    if (is.environment(file_name) || is.list(file_name)) {
+        ilist <- as.list(file_name)
+        for (i in 1:length(ilist)) {
+            assign(names(ilist)[i], ilist[[i]],
+                    pos=FinancialInstrument:::.instrument)
+        }
+        return(invisible(NULL))
+	}
+    if (!is.null(dir) && !dir == "" && substr(dir,nchar(dir),nchar(dir)) != "/")
 		dir <- paste(dir,"/",sep="")
     ssfn <- strsplit(file_name, "\\.")[[1]]
     extension <- if (tolower(tail(ssfn, 1)) %in% c('rda', 'rdata', 'r', 'txt')) {
@@ -104,10 +112,10 @@
     } else {
         tmpenv <- new.env()
         load(paste(dir,file_name,".",extension,sep=""),envir=tmpenv)
-        .instrument <- FinancialInstrument:::.instrument
-        il <- ls(tmpenv$.instrument,all.names=TRUE)
+        il <- ls(tmpenv$.instrument, all.names=TRUE)
         for (i in il) {
-             assign(i, tmpenv$.instrument[[i]], pos=.instrument, inherits=FALSE)
+             assign(i, tmpenv$.instrument[[i]], 
+                    pos=FinancialInstrument:::.instrument, inherits=FALSE)
         }
     }
 }

Modified: pkg/FinancialInstrument/man/saveInstruments.Rd
===================================================================
--- pkg/FinancialInstrument/man/saveInstruments.Rd	2012-03-03 19:19:06 UTC (rev 956)
+++ pkg/FinancialInstrument/man/saveInstruments.Rd	2012-03-04 01:35:01 UTC (rev 957)
@@ -8,9 +8,10 @@
   loadInstruments(file_name = "MyInstruments", dir = "")
 }
 \arguments{
-  \item{file_name}{What to name the file (name of file that
-  holds a .instrument enviroment) Does not include file
-  extension.}
+  \item{file_name}{name of file. e.g.
+  \dQuote{MyInstruments.RData}. As an experimental feature,
+  a \code{list} or \code{environment} can be passed to
+  \code{file_name}.}
 
   \item{dir}{Directory of file (defaults to current working
   directory. ie. "")}



More information about the Blotter-commits mailing list