on.exit
calls code when a function exits, but how and when should I use it?
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
The advantage of
on.exit
is that is gets called when the function exits, regardless of whether an error was thrown. This means that its main use is for cleaning up after risky behaviour. Risky, in this context, usually means accessing resources outside of R (that consequently cannot be guaranteed to work). Common examples include connecting to databases or file (where the connection must be closed when you are finished, even if there was an error), or saving a plot to a file (where the graphics device must be closed afterwards).You can also use
on.exit
for low-risk behaviour with a side effect, such as setting a working directory.Packages that make use of
on.exit
The
withr
package contains manywith_*
functions that change a setting, run some code, then change the setting back. These functions also appear in thedevtools
package.An alternate syntax is found in the
later
package wheredefer
is a convenience wrapper toon.exit
, andscope_*
functions work like thewith_*
functions in the previously mentioned packages.Database connections
In this example,
sqlite_get_query
connects to an sqlite database, ensuring that the connection always gets closed after the query has run. Thecookies
database requires that you have firefox installed on your machine, and you may need to adjust the path to find the cookies file.File connections
In this example,
read_chars
wrapsreadChars
, ensuring that the connection to the file is always closed after reading is finished.Temporary files
The following example adapted from CodeDepends uses a temporary file to save the session history. This temporary file is not needed once the function returns so it is removed.
Saving base graphics
In this example,
my_plot
is a function that creates a plot using base graphics.save_base_plot
accepts a function and a file to save it to, usingon.exit
to ensure that the graphics device is always closed.Setting base graphics options temporarily
In this example,
plot_with_big_margins
callsplot
, overriding the globalmar
ginpar
ameter, usingon.exit
to reset it after the plot is completed.withr
/devtools
equivalent:with_par
Setting global options temporarily
In this example,
create_data_frame
is a function that creates adata.frame
.create_data_frame
ensures that the created object doesn't contain explicit factors.withr
/devtools
equivalent:with_options
later
equivalent:scope_options
Other examples
withr::with_dir
,later::scope_dir
)withr::with_locale
)withr::with_envvars
,later::scope_env_var
)withr::with_libpaths
)withr::with_package
,withr::with_namespace
)