Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
r
communitycreator

What is the on.exit() function in R?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The on.exit() function is used to record a given function to be called when the present or current function exits.

Syntax

The syntax of the on.exit() function is given below:

on.exit(expr = NULL, add = FALSE, after = TRUE)
Syntax of the on.exit() function in R

Parameter values

The on.exit() function takes the following parameter values:

  • expr: This is the expression of the function that needs to be executed. It is a required parameter value.
  • add: This takes a logical value (TRUE or FALSE), which indicates whether the expr is to be executed after any previous expression or overwritten. This is an optional parameter value.
  • after: This takes a logical value (TRUE or FALSE), which indicates if the expr is added on top of previously set expressions. This is an optional parameter value.

Example

# creating functions
function_2 <- function() {
# calling the on.exit() function
on.exit(print('function_two exited'));
print('I am in function_two');
}
# creating another function
function_1 <- function() {
# calling the on.exit() function
on.exit(print('function_one exited'));
print('I am in function_one and am going to call function_two');
# calling functio_two
function_2();
}
# calling the function_1
function_1()
Implementing the on.exit() function

Explanation

  • Line 2: We create a function, function_two .
  • Line 4: Under the function_two function, we call the on.exit() function and pass an expression to it as an argument.
  • Line 6: We pass another expression to the function_two function.
  • Line 10: We create another function, function_one .
  • Line 12: Under the function_one function, we call the on.exit() function and pass an expression to it as an argument.
  • Line 14: We pass another expression to the function_one function.
  • Line 17: We call the function, function_two, while inside function_one.
  • Line 21: We call the function_one function.

In summary, the code written above returned the expression that was passed to the function, function_one, which first executed the print statement and then called function_two . When we called function_two, it returned the expression that was passed to it. Finally, the expressions passed to the on.exit() function in both function_one and function_two were returned.

    RELATED TAGS

    function
    r
    communitycreator

    CONTRIBUTOR

    Onyejiaku Theophilus Chidalu

    Grokking Modern System Design Interview for Engineers & Managers

    Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

    Keep Exploring