Skip to content

An easy way to run the quantstrat faber.R demo within an R function?

2 messages · Andre Mikulec, Brian G. Peterson

#
Hi,I am trying to run the faber.R demo within a function
Here is the urlhttps://r-forge.r-project.org/scm/viewvc.php/pkg/quantstrat/demo/faber.R?view=markup&root=blotter
Within a function, when the 'faber.R code reaches initPortf() the following error is thrown.
Error in exists(paste("portfolio", name, sep = "."), envir = .blotter,  :   object '.blotter' not found
# what I have done to fix is insert the following code # and tested running   # within a function  # within a lexical function  # within R studio ( within a lexical function )
# just after# require(quantstrat) call# required ( If I am running in a function )if(!is.null(sys.call(sys.parent()))) {  if (!exists('.blotter' , envir=.GlobalEnv ))  { .GlobalEnv$.blotter   <- new.env() }   if (!exists('.strategy', envir=.GlobalEnv ))  { .GlobalEnv$.strategy  <- new.env() } }  # just after# getSymbols( . . . ) call# optional: ( If I am running in a function )# does not affect the program ( but for consistency if nothing else )# note: .getSymbols is not an environmentif(!is.null(sys.call(sys.parent()))) {  .GlobalEnv$.getSymbols <- .getSymbols}
# just after # assign(symbol,x) call# required ( If I am running in a function )if(!is.null(sys.call(sys.parent()))) {  assign(symbol,x, envir = .GlobalEnv)}
# RStudio # surrounding any dev() OR chart.#() calls# dev() # Only one RStudio graphics device is permitted# chart # RStudio margins errorif (!any(search() %in% "tools:rstudio")) { # ...}
# Does any other easier way exist, so I do not have to modify# so much code?
Thanks,Andre MikulecAndre_Mikulec at Hotmail.com
#
Please follow the posting guide and do not post in HTML.

Your message is unreadable

http://www.r-project.org/posting-guide.html
On 09/09/2014 08:13 AM, Andre Mikulec wrote: