Elastiques
Elastiques

Reputation: 23

Loading libraries in RStudio?

New to RStudio. Is there a way to efficiently load libraries in scripts so I don’t have to write out the names of the packages for every new script like this…

library(‘ggplot2‘)
library(‘dplyr‘) 
library(‘lubridate‘) 
library(‘tidyr‘)
library(‘stringr‘)

And so on. Thanks in advance.

Upvotes: 2

Views: 701

Answers (2)

Pelishka
Pelishka

Reputation: 111

1) create an .R file with the following, in this example the name of the file will be 'libraries.R'

library(‘ggplot2‘)
library(‘dplyr‘) 
library(‘lubridate‘) 
library(‘tidyr‘)
library(‘stringr‘)

2) source that file in every script you use

source('libraries.R')

Upvotes: 0

RDJ
RDJ

Reputation: 4122

Use this function in each script. Add the names of new libraries as required.

libraries <- c('ggplot2', 'dplyr', 'lubridate', 'tidyr', 'stringr')

lapply(libraries, FUN = function(y) {
    do.call('require', list(y))})

Upvotes: 1

Related Questions