R function to install missing packages

lucacerone

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)) {
    install.packages(pkgname, dependencies = TRUE)
  }
  require(pkgname)
}

As simple as is this function does not work. If I try to run it like ensure_library("dplyr") it installs the package dplyr but then it fails because it trys to import pkgname rather than dplyr in the namespace.

ensure_library("dplyr")
Loading required package: pkgname
Installing package into ‘/home/luca/R-dev’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/src/contrib/dplyr_0.5.0.tar.gz'
Content type 'application/x-gzip' length 708476 bytes (691 KB)
==================================================
downloaded 691 KB

* installing *source* package ‘dplyr’ ...
** package ‘dplyr’ successfully unpacked and MD5 sums checked
** libs

.... a lot of compiling here....

installing to /home/luca/R-dev/dplyr/libs
** R
** data
*** moving datasets to lazyload DB
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (dplyr)

The downloaded source packages are in
    ‘/tmp/Rtmpfd2Lep/downloaded_packages’
Loading required package: pkgname
Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘pkgname’
2: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘pkgname’

Also, if I now re-run it it will install dplyr once again.

I realize this is probably due to R non-standard-evaluation and I have tried several combination of eval/substitute/quote in order to make it work with require but I couldn't succeed.

Can somebody help me understanding what is going on and if there is some easy-fix?

If a function already implementing this exists I would like to know, but what I am really interested is understanding why my code does not work as intended.

IRTFM

Expanding on suggestion to use character.only=TRUE: If you look at the code for require, you see that the first step is only performed when the default value of 'character.only' ( = FALSE) holds:

> require
function (package, lib.loc = NULL, quietly = FALSE, warn.conflicts = TRUE, 
    character.only = FALSE) 
{
    if (!character.only) 
        package <- as.character(substitute(package))
    loaded <- paste("package", package, sep = ":") %in% search()
    if (!loaded) {
        if (!quietly) 
            packageStartupMessage(gettextf("Loading required package: %s", 
                package), domain = NA)
        value <- tryCatch(library(package, lib.loc = lib.loc, 
            character.only = TRUE, logical.return = TRUE, warn.conflicts = warn.conflicts, 

# snipped rest of code

So leaving the default value of character.only in place forces the function to convert the symbol pkgname to a character value.

  as.character(substitute(pkgname))
 [1] "pkgname"

And since 'character.only' is also part of the library logic, and require calls library, you could have used library.

Further comment: You posted a follow-up to Rhelp and got some useful answers from Duncan Murdoch and Peter Dalgaard which clarified (I hope) this question. In the process I wondered whether your resistance to this answer comes about because of an expectation set up by the name of this function that substitution should occur but nothing was happening that looked like "substitution". That expectation seems perfectly reasonable I see now belatedly in retrospect. I think the correct name of the function could have been: substitute_but_only_on_the_basis_of_the_local_environment_or_second_argument. The more common use of substitute is with two arguments:

   y_val=45; a_val=99
   substitute( x + y == z + a , list( y= y_val, a = a_val)
   x + 45 == z + 99

There was no 'effort' to examine the values of any symbol in the first argument unless it had a named item in the second argument (which is named env.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Install missing R packages when running in bash mode

From Dev

r install.packages to install downloaded packages

From Dev

Unable to install R packages

From Dev

Unable to install R packages

From Dev

R (debian): Is there a way to combine install.packages() with auto-apt or similar so that missing dependencies will be installed automatically?

From Dev

Missing WinPE Packages to Install KB4346783?

From Dev

R install packages from Shell

From Dev

pip install -r: install only new packages

From Dev

Cannot install packages to R after brew install

From Dev

R packages: RCurl and curl packages install failure on Linux

From Dev

install.packages("methods") failed for R 3.0.1

From Dev

How to install R-packages not in the conda repositories?

From Dev

R, install.packages('devtools') bug

From Dev

Install R packages from Windows CMD

From Dev

Automatically install list of packages in R if necessary

From Dev

R Install Packages Undable to Access Repository

From Dev

Unable to install R packages in Ubuntu 18.04

From Dev

R can not install packages on centos 6.5

From Dev

Install R Ubuntu 17.10 [Broken Packages]

From Dev

Some R packages won't install

From Dev

Error with install.packages() for R on ubuntu 16.04

From Dev

Passing missing argument from function to function in R

From Dev

conda - How to install R packages that are not available in "R-essentials"?

From Dev

Changes in install.packages() from R 3.1.2 to R 3.2.1

From Dev

Can't install R packages on Linux Mint 17

From Dev

R - How to set the path of install.packages() for shiny server ? - Ubuntu

From Dev

R install.packages() will not work on ubuntu vagrant vm

From Dev

R: Cannot use 0-cloud to install.packages

From Dev

install.packages(“car”) on R 3.0.2 fails in Ubuntu 14.04

Related Related

  1. 1

    Install missing R packages when running in bash mode

  2. 2

    r install.packages to install downloaded packages

  3. 3

    Unable to install R packages

  4. 4

    Unable to install R packages

  5. 5

    R (debian): Is there a way to combine install.packages() with auto-apt or similar so that missing dependencies will be installed automatically?

  6. 6

    Missing WinPE Packages to Install KB4346783?

  7. 7

    R install packages from Shell

  8. 8

    pip install -r: install only new packages

  9. 9

    Cannot install packages to R after brew install

  10. 10

    R packages: RCurl and curl packages install failure on Linux

  11. 11

    install.packages("methods") failed for R 3.0.1

  12. 12

    How to install R-packages not in the conda repositories?

  13. 13

    R, install.packages('devtools') bug

  14. 14

    Install R packages from Windows CMD

  15. 15

    Automatically install list of packages in R if necessary

  16. 16

    R Install Packages Undable to Access Repository

  17. 17

    Unable to install R packages in Ubuntu 18.04

  18. 18

    R can not install packages on centos 6.5

  19. 19

    Install R Ubuntu 17.10 [Broken Packages]

  20. 20

    Some R packages won't install

  21. 21

    Error with install.packages() for R on ubuntu 16.04

  22. 22

    Passing missing argument from function to function in R

  23. 23

    conda - How to install R packages that are not available in "R-essentials"?

  24. 24

    Changes in install.packages() from R 3.1.2 to R 3.2.1

  25. 25

    Can't install R packages on Linux Mint 17

  26. 26

    R - How to set the path of install.packages() for shiny server ? - Ubuntu

  27. 27

    R install.packages() will not work on ubuntu vagrant vm

  28. 28

    R: Cannot use 0-cloud to install.packages

  29. 29

    install.packages(“car”) on R 3.0.2 fails in Ubuntu 14.04

HotTag

Archive