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

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to