invisible Function in R (Example)
In this R tutorial you’ll learn how to change the print mode to of a data object to invisible using the invisible() function.
The article will contain one example for the application of the invisible() function. More precisely, the tutorial contains these contents:
Let’s dig in.
Example: Invisible Print Mode Using invisible() Function
In this example, I’ll illustrate how to avoid printing a data object by applying the invisible function in R.
This can be illustrated based on a user-defined function. Let’s create our own function in R:
my_fun <- function(x) { # Create user-defined function y <- x^2 + x invisible(y) }
As you can see in the previous R code, within our function we have specified a formula based on an input value x. The result of this formula is stored in an output object y. However, within our function we are applying the invisible function to our output object y.
If we apply our user-defined function to a value (i.e. x = 3), nothing is returned:
my_fun(3) # Apply user-defined function
However, our result is just invisible, i.e. not printed to the RStudio console.
If we use the output of our user-defined function in another calculation (i.e. our result plus 10), a result is returned to the RStudio console:
my_fun(3) + 10 # Use output of user-defined function # [1] 22
Video, Further Resources & Summary
I have recently published a video on my YouTube channel, which demonstrates suppress an output using the invisible command based on the R programming code of this tutorial. You can find the video below:
The YouTube video will be added soon.
Furthermore, you may want to read some of the other tutorials on this website:
- print & cat Functions in R
- Print Output of Loop in R
- Print All Data Objects in Workspace in R
- Print Table in R
- Useful Functions in R
- Introduction to R
You have learned in this post how to apply the invisible() function in R programming. If you have additional questions, let me know in the comments section.
Statistics Globe Newsletter