Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Other possible style to use on.exit #1758

Open
hope-data-science opened this issue Feb 3, 2023 · 2 comments
Open

Other possible style to use on.exit #1758

hope-data-science opened this issue Feb 3, 2023 · 2 comments

Comments

@hope-data-science
Copy link

hope-data-science commented Feb 3, 2023

I've learned on.exit in the book and it is really helpful. But setting the parameter add to TRUE get me a little puzzled, and the after argument might just make it harder to know the order of expressions. If the contents in on.exit would just run in the end, why not wrap them up? Which mean:

j08 <- function() {
  on.exit(message("a"), add = TRUE)
  on.exit(message("b"), add = TRUE)
}

could be used as:

j08 <- function() {
    on.exit({message("a");message("b")})
}

In this style, we do not have to figure out which codes would be evaluated first. Wonder if this style would be better.

Thanks.

@jfunction
Copy link

Imagine R keeps a list of commands which need to be executed when exiting. The current implementation allows you to do one of three things:

  • Place commands at the end of the list (add=TRUE, after=TRUE)
  • Place commands at the beginning of the list (add=TRUE, after=FALSE)
  • Replace the list (add=FALSE, after=TRUE or after=FALSE)

What you have suggested is not wrong, and it even makes sense if you only call on.exit once in your program, but the arguments of on.exit are there to support the more general functionality described above.

@hope-data-science
Copy link
Author

I can understand your point, some on.exit might only be executed on condition. In these cases, the argument add and after could be used flexibly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants