2

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.

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Elastiques
  • 23
  • 2

2 Answers2

1

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))})
RDJ
  • 4,052
  • 9
  • 36
  • 54
0

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')
Pelishka
  • 111
  • 4