Re-loading libraries

Hi everyone
I am quite new to R so appologies if the question is very basic
I have a project that loads the following libraries:

install.packages("tidyverse")
library("tidyverse")
install.packages("psych")
library(psych)
install.packages("pastecs")
library(pastecs)
install.packages("gmodels")
library(gmodels)
install.packages("plyr")
library(plyr)
install.packages("magrittr")
library(magrittr)
install.packages("stargazer")
library(stargazer)
install.packages("sjPlot")
library(sjPlot)
library(MatchIt)
library(readxl)
library(sqldf)
library(data.table)

Interestingly, it seems I need to re-load the libraries every time I re-open the project. ie if I save and close the project and come back to it the second day I will need to re-load the packages to run the rest of the code...
Am I missing something or is there a way ro re-load the libraries automatically?
Also if you use a number of libraries frequently, is there a way to make RStudio "pre-load" these libraries automatically every time it starts (even if it is a new project?) One solution I have looked into is to use the pacman library
https://cran.r-project.org/web/packages/pacman/vignettes/Introduction_to_pacman.html

All suggestions are highly appreciated
Kind regards

Hi, you definitely should load packages with, e.g., library(tidyverse) every time you open your new project, but you for sure don't need to install them with install.packages("tidyverse").

The idea of packages is that you only want to load packages that you are actually using, not everything you have installed so far.

As for loading some packages automatically, take a look at this link. Keep in mind that it is not a good idea to load too many packages since your namespace will become bloated and you'll have many bugs that will take hours to debug. Usual pattern is to load only handful of packages (e.g., I only ever load magrittr package for it's pipe operator). Then you load extra packages as you go along in your analysis.

1 Like

You only need to install the packages once (but it is advisable to update them regularly).

You need to load the packages via the library function every time you run a project.

pacman can be a help, but that comes down to personal preferences.

2 Likes