best way to install required packages in r
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.
r package
add a comment |
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.
r package
Usingpacman::p_load()is better than the accepted answer imo.p_loadcan check and install both packages fromCRANandBioConductorcran.r-project.org/web/packages/pacman/vignettes/…
– Tung
Sep 30 '18 at 15:28
1
Thanks Tung. P_load seems a great alternative too.
– R_and_Python_noob
Oct 1 '18 at 12:39
add a comment |
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.
r package
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.
r package
r package
asked Sep 30 '18 at 3:21
R_and_Python_noobR_and_Python_noob
198
198
Usingpacman::p_load()is better than the accepted answer imo.p_loadcan check and install both packages fromCRANandBioConductorcran.r-project.org/web/packages/pacman/vignettes/…
– Tung
Sep 30 '18 at 15:28
1
Thanks Tung. P_load seems a great alternative too.
– R_and_Python_noob
Oct 1 '18 at 12:39
add a comment |
Usingpacman::p_load()is better than the accepted answer imo.p_loadcan check and install both packages fromCRANandBioConductorcran.r-project.org/web/packages/pacman/vignettes/…
– Tung
Sep 30 '18 at 15:28
1
Thanks Tung. P_load seems a great alternative too.
– R_and_Python_noob
Oct 1 '18 at 12:39
Using
pacman::p_load() is better than the accepted answer imo. p_load can check and install both packages from CRAN and BioConductor cran.r-project.org/web/packages/pacman/vignettes/…– Tung
Sep 30 '18 at 15:28
Using
pacman::p_load() is better than the accepted answer imo. p_load can check and install both packages from CRAN and BioConductor cran.r-project.org/web/packages/pacman/vignettes/…– Tung
Sep 30 '18 at 15:28
1
1
Thanks Tung. P_load seems a great alternative too.
– R_and_Python_noob
Oct 1 '18 at 12:39
Thanks Tung. P_load seems a great alternative too.
– R_and_Python_noob
Oct 1 '18 at 12:39
add a comment |
4 Answers
4
active
oldest
votes
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.
Another great way I had not thought of. Thank you.
– R_and_Python_noob
Sep 30 '18 at 3:39
add a comment |
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)
Thank you. I had not realized I could get a list of packages installed at once.
– R_and_Python_noob
Sep 30 '18 at 3:32
add a comment |
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)
add a comment |
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)
Example Code Snippet
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)
Console output
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52574339%2fbest-way-to-install-required-packages-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Another great way I had not thought of. Thank you.
– R_and_Python_noob
Sep 30 '18 at 3:39
add a comment |
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.
Another great way I had not thought of. Thank you.
– R_and_Python_noob
Sep 30 '18 at 3:39
add a comment |
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.
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.
answered Sep 30 '18 at 3:35
PavoDivePavoDive
1,945931
1,945931
Another great way I had not thought of. Thank you.
– R_and_Python_noob
Sep 30 '18 at 3:39
add a comment |
Another great way I had not thought of. Thank you.
– R_and_Python_noob
Sep 30 '18 at 3:39
Another great way I had not thought of. Thank you.
– R_and_Python_noob
Sep 30 '18 at 3:39
Another great way I had not thought of. Thank you.
– R_and_Python_noob
Sep 30 '18 at 3:39
add a comment |
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)
Thank you. I had not realized I could get a list of packages installed at once.
– R_and_Python_noob
Sep 30 '18 at 3:32
add a comment |
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)
Thank you. I had not realized I could get a list of packages installed at once.
– R_and_Python_noob
Sep 30 '18 at 3:32
add a comment |
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)
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)
edited Sep 30 '18 at 3:38
answered Sep 30 '18 at 3:27
kradskrads
567111
567111
Thank you. I had not realized I could get a list of packages installed at once.
– R_and_Python_noob
Sep 30 '18 at 3:32
add a comment |
Thank you. I had not realized I could get a list of packages installed at once.
– R_and_Python_noob
Sep 30 '18 at 3:32
Thank you. I had not realized I could get a list of packages installed at once.
– R_and_Python_noob
Sep 30 '18 at 3:32
Thank you. I had not realized I could get a list of packages installed at once.
– R_and_Python_noob
Sep 30 '18 at 3:32
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Sep 30 '18 at 8:42
George DontasGeorge Dontas
21.4k1687128
21.4k1687128
add a comment |
add a comment |
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)
Example Code Snippet
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)
Console output
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
add a comment |
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)
Example Code Snippet
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)
Console output
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
add a comment |
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)
Example Code Snippet
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)
Console output
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
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)
Example Code Snippet
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)
Console output
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
edited Nov 15 '18 at 17:58
answered Nov 14 '18 at 13:20
Technophobe01Technophobe01
5,22221641
5,22221641
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52574339%2fbest-way-to-install-required-packages-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Using
pacman::p_load()is better than the accepted answer imo.p_loadcan check and install both packages fromCRANandBioConductorcran.r-project.org/web/packages/pacman/vignettes/…– Tung
Sep 30 '18 at 15:28
1
Thanks Tung. P_load seems a great alternative too.
– R_and_Python_noob
Oct 1 '18 at 12:39