KentonWhite / ProjectTemplate

A template utility for R projects that provides a skeletal project.
http://projecttemplate.net
GNU General Public License v3.0
623 stars 159 forks source link

Install package if not yet installed #213

Closed lf-araujo closed 6 years ago

lf-araujo commented 6 years ago

I made a very clean pull request to add the ability to check whether the package is already installed before loading. The problem is I am new to github, and I don't know if the pull reached you. In any case, below is the code if you guys find interesting incorporating it (tested and working locally):

## Load libraries listed in configuration into memory ------------------------
.load.libraries <- function(config, my.project.info) {
  message('Autoloading packages')
  my.project.info$packages <- c()

  for (package.to.load in strsplit(config$libraries, '\\s*,\\s*')[[1]]) {
    message(' Loading package: ', package.to.load)
    #require.package(package.to.load)
    if (package.to.load %in% installed.packages()){
          library(package.to.load, character.only=TRUE)
        } else {
          install.packages(package.to.load)
          library(package.to.load, character.only=TRUE)
        }
    my.project.info$packages <- c(my.project.info$packages, package.to.load)
  }

  return(my.project.info)
}
Hugovdberg commented 6 years ago

The require.package function that was originally in that code snippet should do that already (and some other functionality your code does no longer do). Note that it says require.package, which is defined in require.package.R, and not require which is in base. Did you experience any issues with automatic installation prior to your change to the package? If so, could you change this into a bug report with a clear description of the case in which the function failed?

Hugovdberg commented 6 years ago

Besides, you made the pull request against your own GitHub repository, instead of against this master repo. Usually the create pull request functionality automatically selects the correct base version to commit to, did you change any settings in that screen?

lf-araujo commented 6 years ago

Usually the create pull request functionality automatically selects the correct base version to commit to, did you change any settings in that screen?

Ha! I knew I was doing something wrong! So the tool already support installing packages before loading? I will report back with bug report if the issue is still there..

Thanks anyways...