alathrop
10/19/2016 - 3:33 PM

R package management

general utilities for code management

# Repo used by current R session
getOption("repos")

# check current local library paths
normalizePath(.libPaths(), winslash = "/")

# install to particular library and from particular repo
install.packages(
  pkgs = c("tmaptools"),
  lib = c("C:/Users/AndyLathrop/Documents/R/win-library/3.3"),
  repos = c(CRAN = "https://cran.revolutionanalytics.com"),
  dependencies = TRUE)

# set repo for MRAN
repos.date <- "2016-10-01"
options(repos = c(CRAN = paste("https://mran.revolutionanalytics.com/snapshot/",
        repos.date,sep="")))
        
# set CRAN repo via URL
options(repos=structure(c(CRAN="http://cran.us.r-project.org")))
options(repos = c(CRAN = "https://cran.revolutionanalytics.com"))

# add tidyverse packages
install.packages("tidyverse")
install.packages("sf", repos = "http://cran.us.r-project.org")

library(tidyverse)

# change package (library) path
# <http://bit.ly/2dnGM6T>


# install from particular CRAN snapshot to get latest features ------------

checkpoint::setSnapshot("2018-05-18")

# verify repo snapshot
getOption("repos")
# Repo used by current R session
getOption("repos")

# Current local library
normalizePath(.libPaths(), winslash = "/")

# Local library of packages
installed.packages()[1:10, "Package"] 

# set CRAN repo via URL
options(repos=structure(c(CRAN="http://cran.us.r-project.org")))
options(repos = c(CRAN = "https://cran.revolutionanalytics.com"))

# Install from a particular mirror
install.packages('rmarkdown', repos='http://cran.us.r-project.org')

# set repo for MRAN
repos.date <- "2016-01-01"
options(repos = c(CRAN = paste("https://mran.revolutionanalytics.com/snapshot/",
        repos.date,sep="")))
        
# Add CRAN repos
setRepositories(addURLs =
                c(CRANxtras = "http://cran.us.r-project.org"))
setRepositories(addURLs =
                c(CRAN = "http://cran.us.r-project.org"))
                
# Install from GitHub
if (!require("devtools")) install.packages("devtools")
library(devtools)
devtools::install_github("rstudio/addinexamples", type = "source")
devtools::install_github("tidyverse/ggplot2")

# 'pacman' for package management
## Make sure 'pacman' is installed
#### More info at <https://github.com/trinker/pacman>

# ensure package 'pacman' for package management is installed
if (!require("pacman")) install.packages("pacman")
library(pacman)

# alternate loading from GitHub
# 'devtools' package is required
if (!require("devtools")) install.packages("devtools")
library(devtools)
install_github("trinker/pacman")

# This is how to use 'pacman' in a script
---------------------------------------
#### Packages used in script: 'devtools' for developer functions, 'rmarkdown' 
# for Rmd file creation, 'ggplot2' and 'ggfortify' for plotting. 'ggfortify' 
# requires the package 'devtools' 

#### The 'p_load()' function is the base equivalent of 
# install.packages()' AND 'library()'
pacman::p_load(devtools, 
               rmarkdown, 
               ggplot2, 
               ggfortify, 
               update=TRUE)
pacman::p_loaded()

# ------------------------------

# add tidyverse packages
install.packages("tidyverse")

library(tidyverse)