use withr::local_envvir to set environment variables in a package

I am trying to utilize the withr package to incorporate it's features in my package. I am trying to use withr::local_envvar to set an environment variable in my package so that when the package is unloaded, the environment variable is also unset. I can not figure out how to do this from the withr's documentation.

It seems that this psuedocode should work

<new_item>  <- <something>
withr::local_envvar(new = c(<new_environment_variable> = <new_item>), action = "replace"))

I think that I should be passing in something for the .local_envir parameter but I am not sure what I should send if I want the variable to unset when the package is unloaded.

withr does not do this. It unsets environment variables (and other things) when functions exit.

To set an env var when a package is loaded, use the .onLoad() function, and for unsetting when the package is unloaded, use .onUnload(). See their documentation for the details.

1 Like

Thank you for clearing up my misinterpretation of how this package works.

This topic was automatically closed after 45 days. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.