Hi,
I have been using R for over a year now. I am a very happy user. Thank you
for making this happen.
This is my first question to this list.
I trying to add some functions to quantmod that would enable me to draw
arbitrary lines and text and make sure they are redrawn. I have created
following function:
require(quantmod)
# Add horizontal line to graph produced by quantmod::chart_Series()
add_HorizontalLine<-function(yCoordinatesOfLines, on=1, ...) {
lenv <- new.env()
lenv$add_horizontalline <- function(x, yCoordinatesOfLines, ...) {
xdata <- x$Env$xdata
xsubset <- x$Env$xsubset
x0coords <- rep(1, NROW(yCoordinatesOfLines))
x1coords <- rep(NROW(xdata[xsubset]), NROW(yCoordinatesOfLines))
if ((NROW(x0coords) > 0) & (NROW(x1coords) > 0)) {
segments(x0coords,
yCoordinatesOfLines,
x1coords,
yCoordinatesOfLines, ...)
# abline(h=yCoordinatesOfLines, ...)
}
}
mapply(function(name, value) {assign(name,value,envir=lenv)},
names(list(yCoordinatesOfLines=yCoordinatesOfLines,...)),
list(yCoordinatesOfLines=yCoordinatesOfLines,...))
exp <- parse(text=gsub("list","add_horizontalline",
as.expression(substitute(list(x=current.chob(),
yCoordinatesOfLines=yCoordinatesOfLines, ...)))), srcfile=NULL)
plot_object <- current.chob()
lenv$xdata <- plot_object$Env$xdata
# plot_object$set_frame(sign(on)*abs(on)+1L)
plot_object$set_frame(2*on)
plot_object$add(exp,env=c(lenv, plot_object$Env),expr=TRUE)
plot_object
}
# Short test function that uses add_HorizontalLine
test<-function(series, low=20, high=80) {
chart_Series(SPX, subset="2012")
add_TA(RSI(Cl(SPX)))
plot(add_HorizontalLine(c(low, high), on=2, col=c('green', 'red'),
lwd=2))
}
# Actual test
SPX <- getSymbols("^GSPC", from="2000-01-01", auto.assign=FALSE)
dev.new()
test(SPX)
This gives me the following error:
> test(SPX)
Error in NROW(yCoordinatesOfLines) : object 'low' not found
What am I doing wrong here? Any hints highly appreciated.
The funniest thing is that this was working and somehow broke it...
Best,
Samo
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.