Is it possible to get R to output the value of an expression, that is being calculated within a function? I've attached a very simple example but for more complicated ones would like to be able to debug by seeing what the value of specific expressions are each time it cycles through a loop that executes the expression. I'm relatively new to this so there may be much simpler more elegant ways to do it. For example, is there a command I can put within the function "funct01" that will output the value of z1 to the screen? Thanks Jim =================================## Practice file to try out evaluations y <- 5 (x <- y^2) funct01 <- function (x,y) { z1 <- x + y z2 <- x * y z3 <- x^y results <- data.frame (z1=z1, z2=z2, z3=z3) return(results) } funct01(7,9) ==============================Dr. Jim Maas University of East Anglia Norwich, UK NR4 7TJ [[alternative HTML version deleted]]
Maas James Dr (MED) wrote:> Is it possible to get R to output the value of an expression, that is > being calculated within a function? I've attached a very simple > example but for more complicated ones would like to be able to debug > by seeing what the value of specific expressions are each time it > cycles through a loop that executes the expression. I'm relatively > new to this so there may be much simpler more elegant ways to do it. > > For example, is there a command I can put within the function > "funct01" that will output the value of z1 to the screen?Probably ?cat is what you're looking for. Also look at the ?debug package, or http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/> > Thanks > > Jim > > ================================== ## Practice file to try out > evaluations > > y <- 5 > > (x <- y^2) > > funct01 <- function (x,y) { > > z1 <- x + y z2 <- x * y z3 <- x^y > > results <- data.frame (z1=z1, z2=z2, z3=z3) return(results) > > } > > funct01(7,9) > > > =============================== Dr. Jim Maas University of East > Anglia Norwich, UK NR4 7TJ > > [[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.
Put the line: cat(z1,'\n') in your function. You may also want to put the flush.console() command right after that. 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 Maas James Dr (MED) > Sent: Friday, August 20, 2010 7:41 AM > To: r-help at r-project.org > Subject: [R] output values from within a function > > Is it possible to get R to output the value of an expression, that is > being calculated within a function? I've attached a very simple > example but for more complicated ones would like to be able to debug by > seeing what the value of specific expressions are each time it cycles > through a loop that executes the expression. I'm relatively new to > this so there may be much simpler more elegant ways to do it. > > For example, is there a command I can put within the function "funct01" > that will output the value of z1 to the screen? > > Thanks > > Jim > > =================================> ## Practice file to try out evaluations > > y <- 5 > > (x <- y^2) > > funct01 <- function (x,y) { > > z1 <- x + y > z2 <- x * y > z3 <- x^y > > results <- data.frame (z1=z1, z2=z2, z3=z3) > return(results) > > } > > funct01(7,9) > > > ==============================> Dr. Jim Maas > University of East Anglia > Norwich, UK > NR4 7TJ > > [[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.