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:

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top