Skip to content

on.exit() example does not include add = TRUE when changing multiple options inside a package function #85

@tylermorganwall

Description

@tylermorganwall

In this example in the Code Issues section, add = TRUE is not specified when setting multiple on.exit() calls. This will lead to a the first two on.exit code snippets not being executed and thus failing to reset to the pre-specified par/option settings at the end of the function.


foo <- function(x){
  
  # par():
  oldpar <- par(mfrow = c(2,2))
  on.exit(par(oldpar))
  
  # options():
  oldop <- options(digits = 3)
  on.exit(options(oldop))
  
  # setwd():
  oldwd <- setwd(".")
  on.exit(setwd(oldwd))
  
  # your code which requires a changed option
  
}

This should be changed to the following:


foo <- function(x){
  
  # par():
  oldpar <- par(mfrow = c(2,2))
  on.exit(par(oldpar))
  
  # options():
  oldop <- options(digits = 3)
  on.exit(options(oldop), add = TRUE)
  
  # setwd():
  oldwd <- setwd(".")
  on.exit(setwd(oldwd), add = TRUE)
  
  # your code which requires a changed option
  
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions