[Blotter-commits] r1475 - in pkg/FinancialInstrument: . R inst/tests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon May 27 04:43:06 CEST 2013


Author: gsee
Date: 2013-05-27 04:43:06 +0200 (Mon, 27 May 2013)
New Revision: 1475

Modified:
   pkg/FinancialInstrument/NEWS
   pkg/FinancialInstrument/R/saveInstruments.R
   pkg/FinancialInstrument/inst/tests/test-instrument.R
Log:
 When loadInstruments() is called with a list, it now assigns instruments to their primary_ids found in the instrument instead of to the names of the list.  In addition to being safer, this allows for unnamed lists to be read as easily as named lists.

Modified: pkg/FinancialInstrument/NEWS
===================================================================
--- pkg/FinancialInstrument/NEWS	2013-05-27 02:11:44 UTC (rev 1474)
+++ pkg/FinancialInstrument/NEWS	2013-05-27 02:43:06 UTC (rev 1475)
@@ -23,6 +23,8 @@
   pass the names of columns that are to be used as identifiers instead of as
   normal instrument attributes.  Thanks to Charlie Friedemann for the patch.
 
+* loadInstruments() can now load unnamed lists of instruments because it now
+  uses the primary_ids directly instead of the names of the list/environment
 
 BUG FIXES
 ---------

Modified: pkg/FinancialInstrument/R/saveInstruments.R
===================================================================
--- pkg/FinancialInstrument/R/saveInstruments.R	2013-05-27 02:11:44 UTC (rev 1474)
+++ pkg/FinancialInstrument/R/saveInstruments.R	2013-05-27 02:43:06 UTC (rev 1475)
@@ -93,9 +93,13 @@
     require("utils")
     if (is.environment(file_name) || is.list(file_name)) {
         ilist <- as.list(file_name)
+        if (!all(vapply(ilist, function(x) length(x[["primary_id"]]) == 1L, 
+                        TRUE))) {
+            stop("all instruments must have exactly one primary_id")
+        }
         for (i in seq_along(ilist)) {
-            assign(names(ilist)[i], ilist[[i]],
-                    pos=FinancialInstrument:::.instrument)
+            assign(ilist[[i]][["primary_id"]], ilist[[i]],
+                   pos=FinancialInstrument:::.instrument)
         }
         return(invisible(NULL))
 	}

Modified: pkg/FinancialInstrument/inst/tests/test-instrument.R
===================================================================
--- pkg/FinancialInstrument/inst/tests/test-instrument.R	2013-05-27 02:11:44 UTC (rev 1474)
+++ pkg/FinancialInstrument/inst/tests/test-instrument.R	2013-05-27 02:43:06 UTC (rev 1475)
@@ -25,3 +25,15 @@
   expect_true(!getInstrument("BBB", type="stock", silent=TRUE))
 })
 
+test_that("loadInstruments from list", {
+  rm_instruments(keep.currencies=FALSE)
+  stock(c("A", "B"), currency("USD")) # put some stuff in the .instrument env
+  L <- stock(c("DD", "EE"), "USD", assign_i=FALSE)
+  expect_true(!is.instrument.name("DD"))
+  loadInstruments(L)
+  expect_true(is.instrument.name("DD"))
+  expect_true(is.instrument.name("A"))
+  reloadInstruments(L)
+  expect_true(!is.instrument.name("A"))
+  expect_true(is.instrument.name("DD"))
+})
\ No newline at end of file



More information about the Blotter-commits mailing list