Dear all R gurus, I have following syntax: y = c(1:10) chippy <- function(x) { y[5] = x sin(cos(t(y)%*%y)*exp(-t(y)%*%y/2)) } curve(chippy, 1, 20, n=200) But I am getting error while executing : Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ In addition: Warning message: number of items to replace is not a multiple of replacement length Can anyone tell me how I can recover? Thanks Ron Send instant messages to your online friends http://uk.messenger.yahoo.com [[alternative HTML version deleted]]
Your chipply function is not vectorized. See ?curve and try: curve(Vectorize(chippy)(x), 1, 20, n=200) On 4/18/07, Ron Michael <ron_michael70 at yahoo.com> wrote:> > Dear all R gurus, > > > > I have following syntax: > > > > y = c(1:10) > chippy <- function(x) > { > y[5] = x > sin(cos(t(y)%*%y)*exp(-t(y)%*%y/2)) > } > curve(chippy, 1, 20, n=200) > > > > But I am getting error while executing : > > > > Error in xy.coords(x, y, xlabel, ylabel, log) : > 'x' and 'y' lengths differ > In addition: Warning message: > number of items to replace is not a multiple of replacement length > > > Can anyone tell me how I can recover? > > > > Thanks > > Ron > > > > > > > > > Send instant messages to your online friends http://uk.messenger.yahoo.com > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Dear all R users, Thank you Gabor, and Mark for spending some times on my problem. But I want to move little bit further. I want to show some basic features interactively. I want to move my mouse pointer through the X axis. In graphics window I want to show that, for a particular position of pointer in X-axis, there will be a vertical straightline from that position, which will cull the curve at a point, and from this point another horizontal straight line will be there, which will cut to Y-axis at a particular point. And upon moving my pointer along X-axis, that mapping-point in Y-axis will also move along y-axis accordingly. I also want to mark the position of X-axis point and Y-axis point interactively. I know there is a package "TeachingDemos" for creating this type of interactive picture. But I really dont know how to apply this in my problem. Can anyone give me any suggestion? Your help will be highly appreciated. Thanks and regards, ----- Original Message ---- From: Gabor Grothendieck <ggrothendieck@gmail.com> To: Ron Michael <ron_michael70@yahoo.com> Cc: r-help@stat.math.ethz.ch Sent: Wednesday, April 18, 2007 10:08:32 PM Subject: Re: [R] Problem with ?curve Your chipply function is not vectorized. See ?curve and try: curve(Vectorize(chippy)(x), 1, 20, n=200) On 4/18/07, Ron Michael <ron_michael70@yahoo.com> wrote:> > Dear all R gurus, > > > > I have following syntax: > > > > y = c(1:10) > chippy <- function(x) > { > y[5] = x > sin(cos(t(y)%*%y)*exp(-t(y)%*%y/2)) > } > curve(chippy, 1, 20, n=200) > > > > But I am getting error while executing : > > > > Error in xy.coords(x, y, xlabel, ylabel, log) : > 'x' and 'y' lengths differ > In addition: Warning message: > number of items to replace is not a multiple of replacement length > > > Can anyone tell me how I can recover? > > > > Thanks > > Ron > > > > > > > > > Send instant messages to your online friends http://uk.messenger.yahoo.com > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >Send instant messages to your online friends http://uk.messenger.yahoo.com [[alternative HTML version deleted]]
Something along these lines is on my todo list for the TeachingDemos package, so any thoughts on what you want the user interface to be would be helpful (do you want sliders to set the x-value? Do you want to click on the plot to see an update? Or just moving the mouse over the plot without clicking would update the line?). If you don't mind clicking on the plot, here is a quick example of what you could do (put in your own function and numbers): library(TeachingDemos) myfun <- function(x) sqrt(x) curve( myfun, 0, 10 ) tmp <- cnvrt.coords(0,0, input='plt')$usr tmp2 <- cnvrt.coords(-0.05, -0.05, input='plt')$usr par(xpd=TRUE) while( length(myx <- locator(1)$x) ){ myy <- myfun(myx) curve( myfun, 0, 10 ) lines( c(myx,myx,tmp$x), c(tmp$y, myy, myy), col='green' ) text( c( myx, tmp2$x ), c(tmp2$y, myy), round(c( myx, myy ), 2), col='red') } If you run the above code it will create the plot, then wait for you to click on the plot, when you click it will draw the lines corresponding to the x location of your click and put labels in the margins, clicking again will move the lines and update the labels. Right click and select 'stop' when you are through. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ron Michael > Sent: Thursday, April 19, 2007 1:16 AM > To: r-help at stat.math.ethz.ch > Subject: Re: [R] Problem with ?curve > > Dear all R users, > > Thank you Gabor, and Mark for spending some times on my > problem. But I want to move little bit further. I want to > show some basic features interactively. I want to move my > mouse pointer through the X axis. In graphics window I want > to show that, for a particular position of pointer in X-axis, > there will be a vertical straightline from that position, > which will cull the curve at a point, and from this point > another horizontal straight line will be there, which will > cut to Y-axis at a particular point. And upon moving my > pointer along X-axis, that mapping-point in Y-axis will also > move along y-axis accordingly. I also want to mark the > position of X-axis point and Y-axis point interactively. > > I know there is a package "TeachingDemos" for creating this > type of interactive picture. But I really dont know how to > apply this in my problem. > > Can anyone give me any suggestion? Your help will be highly > appreciated. > > Thanks and regards, > > > ----- Original Message ---- > From: Gabor Grothendieck <ggrothendieck at gmail.com> > To: Ron Michael <ron_michael70 at yahoo.com> > Cc: r-help at stat.math.ethz.ch > Sent: Wednesday, April 18, 2007 10:08:32 PM > Subject: Re: [R] Problem with ?curve > > > Your chipply function is not vectorized. See ?curve and try: > > curve(Vectorize(chippy)(x), 1, 20, n=200) > > > On 4/18/07, Ron Michael <ron_michael70 at yahoo.com> wrote: > > > > Dear all R gurus, > > > > > > > > I have following syntax: > > > > > > > > y = c(1:10) > > chippy <- function(x) > > { > > y[5] = x > > sin(cos(t(y)%*%y)*exp(-t(y)%*%y/2)) > > } > > curve(chippy, 1, 20, n=200) > > > > > > > > But I am getting error while executing : > > > > > > > > Error in xy.coords(x, y, xlabel, ylabel, log) : > > 'x' and 'y' lengths differ > > In addition: Warning message: > > number of items to replace is not a multiple of replacement length > > > > > > Can anyone tell me how I can recover? > > > > > > > > Thanks > > > > Ron > > > > > > > > > > > > > > > > > > Send instant messages to your online friends > http://uk.messenger.yahoo.com > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > > > Send instant messages to your online friends > http://uk.messenger.yahoo.com > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Dear all R users, Thank you Greg for having time in my problem. Your suggestion is working pretty fine for my problem. But here I am involved for some teaching assignments in a local school. If at least I could use sliders for changing values, it would be very fine. Have anyone any suggestion? Thanks and regards, ----- Original Message ---- From: Greg Snow <Greg.Snow@intermountainmail.org> To: Ron Michael <ron_michael70@yahoo.com>; r-help@stat.math.ethz.ch Sent: Thursday, April 19, 2007 11:03:43 PM Subject: RE: [R] Problem with ?curve Something along these lines is on my todo list for the TeachingDemos package, so any thoughts on what you want the user interface to be would be helpful (do you want sliders to set the x-value? Do you want to click on the plot to see an update? Or just moving the mouse over the plot without clicking would update the line?). If you don't mind clicking on the plot, here is a quick example of what you could do (put in your own function and numbers): library(TeachingDemos) myfun <- function(x) sqrt(x) curve( myfun, 0, 10 ) tmp <- cnvrt.coords(0,0, input='plt')$usr tmp2 <- cnvrt.coords(-0.05, -0.05, input='plt')$usr par(xpd=TRUE) while( length(myx <- locator(1)$x) ){ myy <- myfun(myx) curve( myfun, 0, 10 ) lines( c(myx,myx,tmp$x), c(tmp$y, myy, myy), col='green' ) text( c( myx, tmp2$x ), c(tmp2$y, myy), round(c( myx, myy ), 2), col='red') } If you run the above code it will create the plot, then wait for you to click on the plot, when you click it will draw the lines corresponding to the x location of your click and put labels in the margins, clicking again will move the lines and update the labels. Right click and select 'stop' when you are through. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow@intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces@stat.math.ethz.ch > [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Ron Michael > Sent: Thursday, April 19, 2007 1:16 AM > To: r-help@stat.math.ethz.ch > Subject: Re: [R] Problem with ?curve > > Dear all R users, > > Thank you Gabor, and Mark for spending some times on my > problem. But I want to move little bit further. I want to > show some basic features interactively. I want to move my > mouse pointer through the X axis. In graphics window I want > to show that, for a particular position of pointer in X-axis, > there will be a vertical straightline from that position, > which will cull the curve at a point, and from this point > another horizontal straight line will be there, which will > cut to Y-axis at a particular point. And upon moving my > pointer along X-axis, that mapping-point in Y-axis will also > move along y-axis accordingly. I also want to mark the > position of X-axis point and Y-axis point interactively. > > I know there is a package "TeachingDemos" for creating this > type of interactive picture. But I really dont know how to > apply this in my problem. > > Can anyone give me any suggestion? Your help will be highly > appreciated. > > Thanks and regards, > > > ----- Original Message ---- > From: Gabor Grothendieck <ggrothendieck@gmail.com> > To: Ron Michael <ron_michael70@yahoo.com> > Cc: r-help@stat.math.ethz.ch > Sent: Wednesday, April 18, 2007 10:08:32 PM > Subject: Re: [R] Problem with ?curve > > > Your chipply function is not vectorized. See ?curve and try: > > curve(Vectorize(chippy)(x), 1, 20, n=200) > > > On 4/18/07, Ron Michael <ron_michael70@yahoo.com> wrote: > > > > Dear all R gurus, > > > > > > > > I have following syntax: > > > > > > > > y = c(1:10) > > chippy <- function(x) > > { > > y[5] = x > > sin(cos(t(y)%*%y)*exp(-t(y)%*%y/2)) > > } > > curve(chippy, 1, 20, n=200) > > > > > > > > But I am getting error while executing : > > > > > > > > Error in xy.coords(x, y, xlabel, ylabel, log) : > > 'x' and 'y' lengths differ > > In addition: Warning message: > > number of items to replace is not a multiple of replacement length > > > > > > Can anyone tell me how I can recover? > > > > > > > > Thanks > > > > Ron > > > > > > > > > > > > > > > > > > Send instant messages to your online friends > http://uk.messenger.yahoo.com > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@stat.math.ethz.ch 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. > > > > Send instant messages to your online friends > http://uk.messenger.yahoo.com > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >Send instant messages to your online friends http://uk.messenger.yahoo.com [[alternative HTML version deleted]]
Try this for now (a combination of my previous answer and the example from the tkrplot help page): library(tkrplot) library(TeachingDemos) tkcurve <- function(expr, from, to, ...){ tt <- tktoplevel() myx <- from myfun <- function(){ curve(expr, from, to, ...) tmp1 <- cnvrt.coords(0,0, input='plt')$usr tmp2 <- cnvrt.coords(-0.05, -0.05, input='plt')$usr myy <- expr(myx) lines( c(myx,myx,tmp1$x), c(tmp1$y, myy, myy), col='green') par(xpd=TRUE) text( c(myx, tmp2$x), c(tmp2$y, myy), round( c(myx,myy),2 ), col='red') } img <- tkrplot( tt, myfun, hscale=1.5, vscale=1.5 ) f <- function(...) { myxx <- as.numeric(tclvalue("myx")) if (myx != myxx){ myx <<- myxx tkrreplot(img) } } s <- tkscale(tt, command=f, from=from, to=to, variable='myx', showvalue=TRUE, resolution=signif( (to-from)/100, 1 ), orient="horiz") tkpack(img,s) } tkcurve( function(x) sqrt(x), 0.5, 10 ) tkcurve( function(x) sin(x)*x, -2*pi, 4*pi ) ________________________________ From: r-help-bounces@stat.math.ethz.ch on behalf of Ron Michael Sent: Fri 4/20/2007 12:03 AM To: r-help@stat.math.ethz.ch Cc: Greg Snow Subject: Re: [R] Problem with ?curve Dear all R users, Thank you Greg for having time in my problem. Your suggestion is working pretty fine for my problem. But here I am involved for some teaching assignments in a local school. If at least I could use sliders for changing values, it would be very fine. Have anyone any suggestion? Thanks and regards, ----- Original Message ---- From: Greg Snow <Greg.Snow@intermountainmail.org> To: Ron Michael <ron_michael70@yahoo.com>; r-help@stat.math.ethz.ch Sent: Thursday, April 19, 2007 11:03:43 PM Subject: RE: [R] Problem with ?curve Something along these lines is on my todo list for the TeachingDemos package, so any thoughts on what you want the user interface to be would be helpful (do you want sliders to set the x-value? Do you want to click on the plot to see an update? Or just moving the mouse over the plot without clicking would update the line?). If you don't mind clicking on the plot, here is a quick example of what you could do (put in your own function and numbers): library(TeachingDemos) myfun <- function(x) sqrt(x) curve( myfun, 0, 10 ) tmp <- cnvrt.coords(0,0, input='plt')$usr tmp2 <- cnvrt.coords(-0.05, -0.05, input='plt')$usr par(xpd=TRUE) while( length(myx <- locator(1)$x) ){ myy <- myfun(myx) curve( myfun, 0, 10 ) lines( c(myx,myx,tmp$x), c(tmp$y, myy, myy), col='green' ) text( c( myx, tmp2$x ), c(tmp2$y, myy), round(c( myx, myy ), 2), col='red') } If you run the above code it will create the plot, then wait for you to click on the plot, when you click it will draw the lines corresponding to the x location of your click and put labels in the margins, clicking again will move the lines and update the labels. Right click and select 'stop' when you are through. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow@intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces@stat.math.ethz.ch > [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Ron Michael > Sent: Thursday, April 19, 2007 1:16 AM > To: r-help@stat.math.ethz.ch > Subject: Re: [R] Problem with ?curve > > Dear all R users, > > Thank you Gabor, and Mark for spending some times on my > problem. But I want to move little bit further. I want to > show some basic features interactively. I want to move my > mouse pointer through the X axis. In graphics window I want > to show that, for a particular position of pointer in X-axis, > there will be a vertical straightline from that position, > which will cull the curve at a point, and from this point > another horizontal straight line will be there, which will > cut to Y-axis at a particular point. And upon moving my > pointer along X-axis, that mapping-point in Y-axis will also > move along y-axis accordingly. I also want to mark the > position of X-axis point and Y-axis point interactively. > > I know there is a package "TeachingDemos" for creating this > type of interactive picture. But I really dont know how to > apply this in my problem. > > Can anyone give me any suggestion? Your help will be highly > appreciated. > > Thanks and regards, > > > ----- Original Message ---- > From: Gabor Grothendieck <ggrothendieck@gmail.com> > To: Ron Michael <ron_michael70@yahoo.com> > Cc: r-help@stat.math.ethz.ch > Sent: Wednesday, April 18, 2007 10:08:32 PM > Subject: Re: [R] Problem with ?curve > > > Your chipply function is not vectorized. See ?curve and try: > > curve(Vectorize(chippy)(x), 1, 20, n=200) > > > On 4/18/07, Ron Michael <ron_michael70@yahoo.com> wrote: > > > > Dear all R gurus, > > > > > > > > I have following syntax: > > > > > > > > y = c(1:10) > > chippy <- function(x) > > { > > y[5] = x > > sin(cos(t(y)%*%y)*exp(-t(y)%*%y/2)) > > } > > curve(chippy, 1, 20, n=200) > > > > > > > > But I am getting error while executing : > > > > > > > > Error in xy.coords(x, y, xlabel, ylabel, log) : > > 'x' and 'y' lengths differ > > In addition: Warning message: > > number of items to replace is not a multiple of replacement length > > > > > > Can anyone tell me how I can recover? > > > > > > > > Thanks > > > > Ron > > > > > > > > > > > > > > > > > > Send instant messages to your online friends > http://uk.messenger.yahoo.com > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@stat.math.ethz.ch 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. > > > > Send instant messages to your online friends > http://uk.messenger.yahoo.com > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >Send instant messages to your online friends http://uk.messenger.yahoo.com [[alternative HTML version deleted]] ______________________________________________ R-help@stat.math.ethz.ch 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. [[alternative HTML version deleted]]