I'd like to achieve something to the effect of
libs = c("AER", "gbm", "caret", "MASS", "nnet", "randomForest")
for (i in libs) {
if(!is.element(i, .packages()) {
install.packages(i)
}
library(i)
}
The call to library(i) does not replace i with the value being stored in the variable i. Instead, it tries to load the library called "i"
Is there a way to force i to be treated as a variable and resolved before the call to the library?
We can pass an argument to a function while calling the function by simply giving the value as an argument inside the parenthesis. Below is an implementation of a function with a single argument.
Nested Function Calls in R Now consider the arguments: these can be of any type and can have default values inside the function. The latter provides an output even when explicit values are not passed to it. Finally, you can call another function within a function.
Every time you start a new R session you'll need to load the packages/libraries that contain functions you want to use, using either library() or require() . If you load a package with the same function name as in another package, you can use packageName::functionName() to call the function directly.
Open R via your preferred method (icon on desktop, Start Menu, dock, etc.) Click “Packages” in the top menu then click “Install package(s)”. Choose a mirror that is closest to your geographical location. Now you get to choose which packages you want to install.
How about library(...,character.only = TRUE)
?
Here is the full code (combining joran's answer and adding "all.available = TRUE" ).
libs = c("AER", "gbm", "caret", "MASS", "nnet", "randomForest")
for (i in libs){
if( !is.element(i, .packages(all.available = TRUE)) ) {
install.packages(i)
}
library(i,character.only = TRUE)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With