I would like to write an R function that returns silently, like what I get from the barplot
function for example.
I mean that I can store an output in a variable if I do output = myfunction()
, but this output does not get printed if I just use myfunction()
.
We can make the output should not appear. By using invisible() function we can suppress the output.
NULL represents the null object in R. NULL is used mainly to represent the lists with zero length, and is often returned by expressions and functions whose value is undefined. as. null ignores its argument and returns the value NULL .
The invisible() function in R Programming Language is used to make the printing statements invisible which means the printing statements will not appear.
Answer: R returns the last output of a function automatically. We therefore do not need to use the return explicitly. However, using the return command is often considered as good practice, since it makes the R code easier to read and understand.
myFunc <- function(x){ invisible(x*2) } > myFunc(4) > y <-myFunc(4) > y [1] 8 >
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With