I am trying to debug a loop. Is there a way to print the value of a variable that is inside a loop? I have a vector v and inside a loop I have v[i] where i is the index of the loop. Is there a way to see v[i] per loop so that I can see what is going on? Thanks -- View this message in context: http://www.nabble.com/print-value-of-variable-to-screen-or-alert-box--tp23345523p23345523.html Sent from the R help mailing list archive at Nabble.com.
On May 2, 2009, at 7:59 AM, onyourmark wrote:> > I am trying to debug a loop. Is there a way to print the value of a > variable > that is inside a loop? I have a vector v and inside a loop I have > v[i] where > i is the index of the loop. Is there a way to see v[i] per loop so > that I > can see what is going on? > Thanks > --> v <- vector() > for (i in 1:5) { v[i] <- i^2; print(v[i])} [1] 1 [1] 4 [1] 9 [1] 16 [1] 25 > for (i in 1:5) { v[i] <- i^2; print(paste(i, " squared = ", v[i]))} [1] "1 squared = 1" [1] "2 squared = 4" [1] "3 squared = 9" [1] "4 squared = 16" [1] "5 squared = 25" David Winsemius, MD Heritage Laboratories West Hartford, CT
Gabor Grothendieck
2009-May-02 14:51 UTC
[R] print value of variable to screen or alert box?
See ?cat, e.g.> v <- seq(10, 50, 10) > for(i in seq_along(v)) cat("v[", i, "] =", v[i], "\n")v[ 1 ] = 10 v[ 2 ] = 20 v[ 3 ] = 30 v[ 4 ] = 40 v[ 5 ] = 50 On Sat, May 2, 2009 at 7:59 AM, onyourmark <william108 at gmail.com> wrote:> > I am trying to debug a loop. Is there a way to print the value of a variable > that is inside a loop? I have a vector v and inside a loop I have v[i] where > i is the index of the loop. Is there a way to see v[i] per loop so that I > can see what is going on? > Thanks > -- > View this message in context: http://www.nabble.com/print-value-of-variable-to-screen-or-alert-box--tp23345523p23345523.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Well there are always the print and cat functions which print to standard output. You may also want to look at flush.console if buffering is a problem. Also using "\b" or "\r" with cat can keep from producing too much output. There are also the progress bar functions (winProgressBar, tkProgressBar, txtProgressBar) which show progress bars but also allow for labels that can be changed inside the loop, the bar could show i and the label could show v[i]. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of onyourmark > Sent: Saturday, May 02, 2009 5:59 AM > To: r-help at r-project.org > Subject: [R] print value of variable to screen or alert box? > > > I am trying to debug a loop. Is there a way to print the value of a > variable > that is inside a loop? I have a vector v and inside a loop I have v[i] > where > i is the index of the loop. Is there a way to see v[i] per loop so that > I > can see what is going on? > Thanks > -- > View this message in context: http://www.nabble.com/print-value-of- > variable-to-screen-or-alert-box--tp23345523p23345523.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Bill.Venables at csiro.au
2009-May-03 02:02 UTC
[R] print value of variable to screen or alert box?
print(c(i, v[i])) Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of onyourmark Sent: Saturday, 2 May 2009 9:59 PM To: r-help at r-project.org Subject: [R] print value of variable to screen or alert box? I am trying to debug a loop. Is there a way to print the value of a variable that is inside a loop? I have a vector v and inside a loop I have v[i] where i is the index of the loop. Is there a way to see v[i] per loop so that I can see what is going on? Thanks -- View this message in context: http://www.nabble.com/print-value-of-variable-to-screen-or-alert-box--tp23345523p23345523.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.