[R] Help with non standard evaluation and require function

Duncan Murdoch murdoch.duncan at gmail.com
Fri Aug 12 18:18:48 CEST 2016

On 12/08/2016 11:57 AM, Luca Cerone wrote:
> Hi everybody,
> I am having a hard time in understanding how to deal with non standard
> evaluation and the require function.
> I asked about it on Stackoverflow at
> http://stackoverflow.com/questions/38922012/r-function-to-install-missing-packages,
> below you can find my question.
> Thanks a lot for the help!
> Cheers,
> Luca
> For one of my scripts I want to write an R function that checks if a
> package is already installed: if so it should use library() to import
> it in the namespace, otherwise it should install it and import it.
> I assumed that pkgname is a string and tried to write something like:
> ensure_library <- function(pkgname) {
>    if (!require(pkgname)) {

Should be

   if (!require(pkgname, character.only = TRUE)) {

>      install.packages(pkgname, dependencies = TRUE)
>    }
>    require(pkgname)

Similarly here.  You won't need this for the install.packages call.

You should have been able to figure this out from the ?require help 
page.  It is mentioned twice, and is used in one of the examples.

Duncan Murdoch

More information about the R-help mailing list