Is there a way to print or display the value of a variable while inside a function, as opposed to printing the value outside the function after the function has been called?
I am virtually certain there is and thought the code was called reveal or something similar, but I cannot recall the correct term.
my.function <- function(x) {
y <- x^2
# reveal(y)
# display(y)
# desired result is to print or display here:
# [1] 16
cat(y)
print(y)
return(y)
}
x <- 4
my.function(x)
#16[1] 16
#[1] 16
cat(y), print(y) and return(y) all print outside the function. Thank you for any advice.
EDIT
I found a similar question here:
https://stat.ethz.ch/pipermail/r-help/2002-November/027348.html
The response to that question from Peter Dalgaard was to uncheck an option called buffered output under the Misc tab. However, that does not seem to be working in my case. Perhaps the questions are unrelated.
4has not even been passed to the function...}.4to the function before defining it.?debug, which will step through the function and allow you to inspect objects at different points in the function's execution. To give it a go, trydebug(my.function); my.function(4), and then enteryat any point after its definition and before the call toreturn.messagewhich does a good job of "printing from within functions" -- I've used it for debugging with parallel operations, too.