Skip to content

Instantly share code, notes, and snippets.

@skngetich
Created March 19, 2022 09:22
Show Gist options
  • Save skngetich/b5550975aaf4ae2304370c0e836d5a3a to your computer and use it in GitHub Desktop.
Save skngetich/b5550975aaf4ae2304370c0e836d5a3a to your computer and use it in GitHub Desktop.
This code snippet code install packages if it does not exist
Clear all variables in work space and install packages
```{r message = FALSE,warning = FALSE}
# Clears environment
rm(list=ls())
# Add packages needed in the project
requiredPackages = c('quantmod','TTR','tseries')
for(p in requiredPackages){
if(!require(p,character.only = TRUE)) install.packages(p) # Install package if it does not exist
library(p,character.only = TRUE)
}
```
@skngetich
Copy link
Author

Alternative Method 2:

# Package names
packages <- c("ggplot2", "readxl", "dplyr", "tidyr", "ggfortify", "DT", "reshape2", "knitr", "lubridate")

# Install packages not yet installed
installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))

@skngetich
Copy link
Author

Alternative Method 3:

install.packages("pacman")
pacman::p_load(ggplot2, tidyr, dplyr)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment