R/general.R

#' Library0 Function
#'
#' This function allows you to load/install-and-load multiple packages
#' @param packages vector of packages that you want to load
#' @keywords R packages
#' @export
#' @examples
#' library0(c("plyr", "dplyr", "ggplot2"))
#'
library0 <- function(packages){

      suppressWarnings({suppressMessages({
            for(x in packages){
                  if(x %in% rownames(installed.packages())){
                        try({
                              library(x, character.only = T)
                              # print(paste0("Successfully loaded: ", x))
                        })
                  }else{
                        try({
                              install.packages(x, dependencies = T, repos = "https://wbc.upm.edu.my/cran/");
                              library(x, character.only = T)
                        })
                  }

                  if(!(x %in% rownames(installed.packages()))){
                        print(paste0("Error installing: ", x))
                        stop("Packages not fully loaded!")
                  }
            }
      })})
}

#' Library1 Function
#'
#' (Simplified version of library0) This function allows you to load/install-and-load multiple packages
#' @param packages vector of packages that you want to load
#' @keywords R packages
#' @export
#' @examples
#' library0(c("plyr", "dplyr", "ggplot2"))
#'
library1 <- function(packages){
      
      suppressWarnings({suppressMessages({
            for(x in packages){
                  if(x %in% rownames(installed.packages())){
                        try({
                              library(x, character.only = T)
                              # print(paste0("Successfully loaded: ", x))
                        })
                  }else{
                        try({
                              install.packages(x, dependencies = T)
                              library(x, character.only = T)
                        })
                  }
                  
                  if(!(x %in% rownames(installed.packages()))){
                        print(paste0("Error installing: ", x))
                        stop("Packages not fully loaded!")
                  }
            }
      })})
}

#' pause function
#'
#' This function allows you to pause a Rexec script
#' @param option different option allows you to prompt different message
#' @keywords pause option
#' @export
#' @examples
#' pause()
#'
pause <- function(option = 99){

      if(option == 1){
            prompt1 <- "Input 1 to text file to SAVE, Otherwise Input 0 \n ...then Press <Enter> to continue...\n"

      }else if(option == 2){
            prompt1 <- "hi..."

      }else if(option == 3){
            prompt1 <- "Input correct date to move csv files...\n";
      }else{
            prompt1 <- "Press <Enter> to continue..."
      }

      if (interactive()) {
            invisible(readline(prompt = prompt1))
      }
      else {
            cat(prompt1)
            invisible(readLines(file("stdin"), 1))
      }
}



#' formatdir.f function
#'
#' This function formats adds backslash to directory string without backslash
#' @param dir directory
#' @keywords format directory backslash
#' @export
#' @examples
#' formatdir.f(dir = "C:/Users/User/tempmplus")
#'

formatdir.f <- function(dir){
      n <- nchar(dir)
      
      if( substr(dir, n, n) == "/" ){
            return(dir)
            
      }else{
            return(paste0(dir, "/"))
      }
      
}

#' libraryf function
#'
#' This function loads the library if it is not loaded
#' @param rpackage r package to load (character vector)
#' @keywords libraryf load library if not loaded
#' @export
#' @examples
#' libraryf("function0", "plyr", "dplyr")
#'

libraryf <- function(rpackage){
    for(x in rpackage){
      if( !(x %in% (.packages())) ){
            library(x, character.only = T, quietly = T)
      }
    }
}
junyitt/function0 documentation built on May 20, 2019, 11:16 a.m.