library(proto)
library(gsubfn)
library(tidyr)
library(dplyr)
library(ggplot2)
library(stringr)
library(magrittr)
library(usmap)
library(RCurl)
library(RJSONIO)
library(sqldf)
For the list of libraries above I did not have corresponding line of code for package installations. I ended up googling the package names and installing them manually.
I am curious what's the best way to install all the required packages when you have a long list of library for your code set and you are not sure which are already installed in your work space or just don't know what packages to install.
Do you use require() function? Not sure if I would want to change the function to load package if the original author would have used install.package() function initially.
I would like to know a more efficient way to getting the packages installed without having to manually google and install them.
Simply enclose the quoted package names in c()
for example:
pkgs <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
"stringr","magrittr","usmap","RCurl","RJSONIO","sqldf")
# Install:
install.packages(pkgs)
Then, if you also want to load the packages:
# Load:
lapply(pkgs, require, character.only = TRUE)
Check out the librarian
package.
# attach packages to the search path, installing them from CRAN or GitHub if needed
librarian::shelf(plyr, tidyverse, knitr, ggplot2, scales, sqldf)
# List of all loaded packages
# (.packages())
librarian:::check_attached()
# unload
librarian::unshelf(plyr, tidyverse, knitr, ggplot2, scales, reshape2, also_depends = TRUE)
# print(.Last.value)
are you looking for something like this?
listOfPackages <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
"stringr","magrittr","usmap","RCurl",
"RJSONIO","sqldf")
for (i in listOfPackages){
if(! i %in% installed.packages()){
install.packages(i, dependencies = TRUE)
}
require(i)
}
You can load a package with either library
or require
. The last one will not force the loading, if the package is already load, while the first one will.
Personally, I prefer to use the code snippet below, this only installs the packages that are not currently installed [ Saves lots of time ], while subsequently loading all the listed packages.
I would also recommend you load the package dependencies explicitly via the call to install.packages(<package list, dependencies = TRUE)
requiredPackages <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
"stringr","magrittr","usmap","RCurl","RJSONIO","sqldf")
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
ipak(requiredPackages)
On the first call, everything is either installed and/or loaded, on the second run everything is loaded if not already loaded.
> requiredPackages <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
+ "stringr","magrittr","usmap","RCurl","RJSONIO","sqldf")
> ipak <- function(pkg){
+ new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
+ if (length(new.pkg))
+ install.packages(new.pkg, dependencies = TRUE)
+ sapply(pkg, require, character.only = TRUE)
+ }
> ipak(requiredPackages)
proto gsubfn tidyr dplyr ggplot2 stringr magrittr usmap
TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
RCurl RJSONIO sqldf
TRUE TRUE TRUE
Install only Packages that are not already available in the system. Followed by loading all the required packages.
#Installing Packages that are not already available in the system
list.of.packages <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
"stringr","magrittr","usmap","RCurl","RJSONIO","sqldf")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
#Loading Packages
invisible(lapply(list.of.packages, require, character.only = TRUE))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.