Samo Pahor
2012-Feb-11 14:43 UTC
[R] object not found - Can not figure out why I get this error: Error in NROW(yCoordinatesOfLines) : object 'low' not found
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]]
ilai
2012-Feb-12 22:28 UTC
[R] object not found - Can not figure out why I get this error: Error in NROW(yCoordinatesOfLines) : object 'low' not found
Ah, scoping rules... Consider: f <- function(x,...) plot(x,xlim=c(low,high),...) f(1:10,low=2,high=9) # "Error ... object 'low' not found " But: f <- function(x,low,high,...) plot(x,xlim=c(low,high),...) f(1:10,2,9,col=2) # beautiful red points [low,high] Sorry I can't be more specific but never heard of quantmod. Hope this helps anyway. On Sat, Feb 11, 2012 at 7:43 AM, Samo Pahor <samo.pahor at gmail.com> wrote:> 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]] > > ______________________________________________ > R-help at r-project.org 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.