R/install.apps.R

Defines functions install.apps

install.apps <- function(pkg, repos = getOption("repos"), applib = getOption("applib"), type = getOption("pkgType"), ...){
  
  #contrib.url will prompt for a mirror if needed
  mycontrib <- force(contrib.url(repos, type));
  
  #input validation
  stopifnot(length(pkg) == 1);
  db <- available.packages(contriburl = mycontrib);
  if(!(pkg %in% row.names(db))){
    stop("Package ", pkg, "not available from this repository.")
  };
  
  #the application will be installed in its own dir
  fullname <- paste(pkg, db[pkg,"Version"], sep="_");
  fullpath <- file.path(applib, fullname);
  if(file.exists(fullpath)){
    stop("App already exists: ", fullpath);
  }

  #create the application dir
  message("Installing app in: ", fullpath);
  stopifnot(dir.create(fullpath));     
     
  #install in lib
  inlib(fullpath, {
    #install the packages
    install.packages(pkg, available=db, lib=fullpath, contriburl = mycontrib, dependencies=TRUE, ...);
    
    #autocreate a LIBRARY file
    newdb <- installed.packages();
    alldep1 <- package_dependencies(pkg, db=newdb, recursive=TRUE)[[pkg]];
    alldep2 <- package_dependencies(pkg, db=newdb, recursive=FALSE, which="Suggests")[[pkg]];
    alldep3 <- unname(unlist(package_dependencies(alldep2, db=newdb, recursive=TRUE)));
    alldep <- sort(unique(c(pkg, alldep1, alldep2, alldep3)));
    
    #Filter base packages
    alldep <- alldep[!(alldep %in% basepkgs())];
    
    #Filter packages that failed to install
    alldep <- alldep[alldep %in% row.names(newdb)];    
    
    #get versions    
    allvers <- newdb[alldep, "Version"];
    libstring <- paste(alldep, "==", allvers);
    writeLines(libstring, file.path(fullpath, "LIBRARY"));
    cat(paste("Application: ", fullname), "-------------------------", libstring, sep="\n");    
  });
}
jeroenooms/apps documentation built on May 17, 2017, 6:17 p.m.