Check if R package is installed then load library

前端 未结 6 949
半阙折子戏
半阙折子戏 2020-12-15 07:49

Our R scripts are used on multiple users on multiple computers and hence there are deviations in which packages are installed on each computer. To ensure that each script wo

6条回答
  •  一整个雨季
    2020-12-15 08:07

    I wrote this function the other day that I thought would be useful...

    install_load <- function (package1, ...)  {   
    
       # convert arguments to vector
       packages <- c(package1, ...)
    
       # start loop to determine if each package is installed
       for(package in packages){
    
           # if package is installed locally, load
           if(package %in% rownames(installed.packages()))
              do.call('library', list(package))
    
           # if package is not installed locally, download, then load
           else {
              install.packages(package)
              do.call("library", list(package))
           }
       } 
    }
    

提交回复
热议问题