[Blotter-commits] r956 - in pkg/FinancialInstrument: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Mar 3 20:19:06 CET 2012
Author: gsee
Date: 2012-03-03 20:19:06 +0100 (Sat, 03 Mar 2012)
New Revision: 956
Modified:
pkg/FinancialInstrument/DESCRIPTION
pkg/FinancialInstrument/R/saveInstruments.R
pkg/FinancialInstrument/man/saveInstruments.Rd
Log:
- If saveInstrument is given a file name that ends with ".R" or ".txt" it will
create a file of R code that can be used (sourced) to recreate the
.instrument environment.
- loadInstrument also supports .R and .txt files
- removed extension formal
Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION 2012-03-03 13:11:39 UTC (rev 955)
+++ pkg/FinancialInstrument/DESCRIPTION 2012-03-03 19:19:06 UTC (rev 956)
@@ -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.3
+Version: 0.12.4
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 13:11:39 UTC (rev 955)
+++ pkg/FinancialInstrument/R/saveInstruments.R 2012-03-03 19:19:06 UTC (rev 956)
@@ -16,18 +16,26 @@
#' Save and Load all instrument definitions
#'
#' Saves (loads) the .instrument environment to (from) disk.
-#' \code{loadInstruments} will add instrument definitions that were saved to
-#' your .instrument environment
#'
#' After you have defined some instruments, you can use \code{saveInstruments}
#' to save the entire .instrument environment to disk.
+#'
+#' \code{loadInstruments} will read a file that contains instruments and add
+#' those instrument definitions to your .instrument environment
#'
+#' The \code{file_name} should have a file extension of \dQuote{RData},
+#' \dQuote{rda}, \dQuote{R}, or \dQuote{txt}. If the \code{file_name} does not
+#' end with one of those, \dQuote{.RData} will be appended to the
+#' \code{file_name}
+#'
+#' If the file extension is \dQuote{R} or \dQuote{txt}, \code{saveInstruments}
+#' will create a text file of \R code that can be \code{\link{source}}d to
+#' 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 dir Directory of file (defaults to current working directory. ie. "")
-#' @param extension File extension of file. default is RData. This will be ignored if
-#' \code{file_name} ends with either \sQuote{.rda} or \sQuote{.RData}.
#' @return Called for side-effect
#' @author Garrett See
#' @seealso save, load load.instrument define_stocks, define_futures,
@@ -35,43 +43,73 @@
#' @examples
#'
#' \dontrun{
-#' stock("SPY","USD",1)
-#' saveInstruments()
-#' loadInstruments()
+#' stock("SPY", currency("USD"), 1)
+#' tmpdir <- tempdir()
+#' saveInstruments("MyInstruments.RData", dir=tmpdir)
+#' rm_instruments(keep.currencies=FALSE)
+#' loadInstruments("MyInstruments.RData", dir=tmpdir)
+#' # write .R file that can be sourced
+#' saveInstruments("MyInstruments.R", dir=tmpdir)
+#' rm_instruments(keep.currencies=FALSE)
+#' loadInstruments("MyInstruments.R", dir=tmpdir)
+#' #source(file=paste(tmpdir, "MyInstruments.R", sep="/")) # same
+#' unlink(tmpdir, recursive=TRUE)
#' }
#' @export
#' @rdname saveInstruments
-saveInstruments <- function(file_name="MyInstruments", dir="", extension="RData") {
+saveInstruments <- function(file_name="MyInstruments", dir="") {
if (!is.null(dir) && !dir == "" && substr(dir,nchar(dir),nchar(dir)) != "/")
dir <- paste(dir,"/",sep="")
- #.instrument <- get('FinancialInstrument:::.instrument', pos=.GlobalEnv)
.instrument <- FinancialInstrument:::.instrument
ssfn <- strsplit(file_name, "\\.")[[1]]
- if(any(tail(ssfn, 1) == c("rda", "RData"))) {
+ extension <- if (tolower(tail(ssfn, 1)) %in% c('rda', 'rdata', 'r', 'txt')) {
file_name <- paste(ssfn[1:(length(ssfn)-1)], collapse=".")
- extension <- tail(ssfn, 1)
- }
- save(.instrument,file=paste(dir,file_name,".",extension,sep=""))
+ tail(ssfn, 1)
+ } else "RData"
+ file.name <- paste(dir, file_name, ".", extension, sep="")
+ # if extension is "R", "r", or "txt" create a file that can be sourced
+ if (tolower(extension) %in% c("r", "txt")) {
+ cat("#auto-generated by FinancialInstrument:::saveInstruments\n\n",
+ "require(FinancialInstrument)\n\n", file=file.name)
+ for (s in ls_instruments()) {
+ cat('assign("', s, '", pos=FinancialInstrument:::.instrument, ',
+ 'value=\n', file=file.name, sep="", append=TRUE)
+ sink(file.name, append=TRUE)
+ dput(getInstrument(s))
+ sink()
+ cat(")\n\n", file=file.name, append=TRUE)
+ #system(paste("cat", file.name)) #for debugging
+ }
+ } else save(.instrument, file = file.name)
}
+
#' @export
#' @rdname saveInstruments
-loadInstruments <-function(file_name="MyInstruments", dir="", extension="RData") {
+loadInstruments <-function(file_name="MyInstruments", dir="") {
require("utils")
if (!is.null(dir) && !dir == "" && substr(dir,nchar(dir),nchar(dir)) != "/")
dir <- paste(dir,"/",sep="")
- tmpenv <- new.env()
ssfn <- strsplit(file_name, "\\.")[[1]]
- if(any(tail(ssfn, 1) == c("rda", "RData"))) {
+ extension <- if (tolower(tail(ssfn, 1)) %in% c('rda', 'rdata', 'r', 'txt')) {
file_name <- paste(ssfn[1:(length(ssfn)-1)], collapse=".")
- extension <- tail(ssfn, 1)
+ tail(ssfn, 1)
+ } else "RData"
+ file.name <- paste(dir, file_name, ".", extension, sep="")
+ if (tolower(extension) %in% c("r", "txt")) {
+ if (substr(readLines(file.name, 1L), 1, 5) != "#auto") {
+ warning(paste(file.name, "was not created by 'saveInstruments'"))
+ }
+ source(file.name)
+ } else {
+ tmpenv <- new.env()
+ load(paste(dir,file_name,".",extension,sep=""),envir=tmpenv)
+ .instrument <- FinancialInstrument:::.instrument
+ il <- ls(tmpenv$.instrument,all.names=TRUE)
+ for (i in il) {
+ assign(i, tmpenv$.instrument[[i]], pos=.instrument, inherits=FALSE)
+ }
}
- load(paste(dir,file_name,".",extension,sep=""),envir=tmpenv)
- .instrument <- FinancialInstrument:::.instrument
- il <- ls(tmpenv$.instrument,all.names=TRUE)
- for (i in il) {
- assign(i, tmpenv$.instrument[[i]], pos=.instrument, inherits=FALSE)
- }
}
Modified: pkg/FinancialInstrument/man/saveInstruments.Rd
===================================================================
--- pkg/FinancialInstrument/man/saveInstruments.Rd 2012-03-03 13:11:39 UTC (rev 955)
+++ pkg/FinancialInstrument/man/saveInstruments.Rd 2012-03-03 19:19:06 UTC (rev 956)
@@ -3,11 +3,9 @@
\alias{saveInstruments}
\title{Save and Load all instrument definitions}
\usage{
- saveInstruments(file_name = "MyInstruments", dir = "",
- extension = "RData")
+ saveInstruments(file_name = "MyInstruments", dir = "")
- loadInstruments(file_name = "MyInstruments", dir = "",
- extension = "RData")
+ loadInstruments(file_name = "MyInstruments", dir = "")
}
\arguments{
\item{file_name}{What to name the file (name of file that
@@ -16,29 +14,46 @@
\item{dir}{Directory of file (defaults to current working
directory. ie. "")}
-
- \item{extension}{File extension of file. default is
- RData. This will be ignored if \code{file_name} ends with
- either \sQuote{.rda} or \sQuote{.RData}.}
}
\value{
Called for side-effect
}
\description{
Saves (loads) the .instrument environment to (from) disk.
- \code{loadInstruments} will add instrument definitions
- that were saved to your .instrument environment
}
\details{
After you have defined some instruments, you can use
\code{saveInstruments} to save the entire .instrument
environment to disk.
+
+ \code{loadInstruments} will read a file that contains
+ instruments and add those instrument definitions to your
+ .instrument environment
+
+ The \code{file_name} should have a file extension of
+ \dQuote{RData}, \dQuote{rda}, \dQuote{R}, or
+ \dQuote{txt}. If the \code{file_name} does not end with
+ one of those, \dQuote{.RData} will be appended to the
+ \code{file_name}
+
+ If the file extension is \dQuote{R} or \dQuote{txt},
+ \code{saveInstruments} will create a text file of \R code
+ that can be \code{\link{source}}d to load instruments
+ back into the .instrument environment.
}
\examples{
\dontrun{
-stock("SPY","USD",1)
-saveInstruments()
-loadInstruments()
+stock("SPY", currency("USD"), 1)
+tmpdir <- tempdir()
+saveInstruments("MyInstruments.RData", dir=tmpdir)
+rm_instruments(keep.currencies=FALSE)
+loadInstruments("MyInstruments.RData", dir=tmpdir)
+# write .R file that can be sourced
+saveInstruments("MyInstruments.R", dir=tmpdir)
+rm_instruments(keep.currencies=FALSE)
+loadInstruments("MyInstruments.R", dir=tmpdir)
+#source(file=paste(tmpdir, "MyInstruments.R", sep="/")) # same
+unlink(tmpdir, recursive=TRUE)
}
}
\author{
More information about the Blotter-commits
mailing list