1 Package manager

1.1 pacman

  • Install pacman package in R. In order to use the functions that are contained in the pacman package, we have to install and load the package to R:
install.packages("pacman")    # Install pacman package
library("pacman")             # Load pacman package
  • After running the previous lines of code, we can use the functions provided by the pacman package.
1.1.1 Example 1: Install & load multiple R packages using p_load function
  • In this example, I will illustrate how to apply the p_load function to load multiple add-on packages in one line of code.

  • Have a look at the following R syntax:

p_load(dplyr, stringr)   # Install & load packages
  • After executing the previous code, we have loaded the packages dplyr, and stringr.

  • Note that the p_load function also checks whether a package is installed already. If the package is not installed yet, it is installed automatically by the pacman package.

1.1.2 Example 2: Unload multiple R packages using p_unload function
  • Another useful function of the pacman package is the p_unload function. The p_unload function can be used to detach one or multiple loaded packages from within R.

  • Let’s apply the p_unload command to unload the packages dplyr, and stringr that we have loaded before:

p_unload(dplyr, stringr)  # Unload packages
  • The following packages have been unloaded: dplyr, stringr The previous output of the RStudio console tells us that our three packages were detached.
1.1.3 Example 3: Update Outdated R Packages Using p_update Function
  • In this example, I will show how to use the p_update function of the pacman package to check for outdated packages AND to update all of these packages.

  • First, let’s check which of my packages are outdated:

p_update(update = FALSE)  # Check for outdated packages
  • Second, the pacman package provides an easy solution to how we can update all outdated packages with only one line of R code.

  • Make sure that you have some time before running the following R code. This may take some time, depending on the number of packages you need to update:

p_update() # Update all packages

2 Data Management

2.1 devtools

  • Install the released version of remotes from CRAN:
install.packages("devtools")
  • Install a package from CRAN using devtools
devtools::install_cran("MASS")
  • This command installs the “MASS” package from CRAN.

2.2 remotes

  • Install the released version of remotes from CRAN:
install.packages("remotes")
  • To install the latest version of a package in the default branch from GitHub, you can use the user/repo form. Note that user can also be an organization:
remotes::install_github("r-lib/conflicted")

2.3 stargazer

  • Install the released version of remotes from CRAN:
install.packages("stargazer")
  • Create Summary Statistics Table Using Stargazer
# Load your external dataset
data(mtcars)
# Check the summary of dataset
summary(mtcars)
# Create summary statistics table using stargazer
stargazer(mtcars, type='text', title='Summary Statistics')
  • Fit a Regression Model and exported to the specified file paths
# Fit regression model
fit <- lm(mpg ~ wt + cyl, data = mtcars)
# Create table that summarizes the new regression model
stargazer(fit, type = 'text', title = 'Regression Summary Table', 
           out = 'C:\\Users\\Downloads\\R.txt')

2.4 readxl

  • Install readxl from CRAN:
install.packages("readxl")
  • read_excel() reads both xls and xlsx files and detects the format from the extension
xlsx_example <- readxl_example("datasets.xlsx")
read_excel(xlsx_example)
  • List the sheet names with excel_sheets()
excel_sheets(xlsx_example)
  • Specify a worksheet by name or number
read_excel(xlsx_example, sheet = "chickwts")
read_excel(xls_example, sheet = 4)

2.5 here

  • here() uses a reasonable heuristics to find your project’s files, based on the current working directory at the time when the package is loaded. Use it as a drop-in replacement for file.path(), it will always locate the files relative to your project root.
install.packages("here")
  • Use project-relative paths. You can build a path relative to the top-level directory in order to build the full path to a file:
here("data", "penguins.csv")

2.6 rvest

  • rvest is a package that makes it easy to scrape (or harvest) data from html web pages.
install.packages("rvest")
  • If the page contains tabular data you can convert it directly to a data frame with html_table():
html <- read_html("https://en.wikipedia.org/w/index.php?title=The_Lego_Movie&oldid=998422565")
html %>% 
  html_element(".tracklist") %>% 
  html_table()

2.7 ggthemes

  • Some extra themes, geoms, and scales for ‘ggplot2’. Provides ‘ggplot2’ themes and scales that replicate the look of plots.
install.packages('ggthemes')
  • Example: Theme_base() in ggtheme in R
ggplot(mtcars) + geom_point(aes(x = wt, y = mpg, 
                    colour = factor(gear))) + theme_base() + ggtitle("theme_base()") 
  • Example: theme_calc() in ggtheme in R
ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(gear))) +
  geom_point() +
  theme_calc() +
  ggtitle("theme_calc()")

2.8 ggpubr

  • ggpubr is‘ggplot2’ Based Publication Ready Plots
install.packages("ggpubr")
  • Example: Distribution
ggdensity(wdata, x = "weight",
   add = "mean", rug = TRUE,
   color = "sex", fill = "sex",
   palette = c("#00AFBB", "#E7B800"))

2.9 plotly

  • plotly is an R package for creating interactive web-based graphs via the open source JavaScript graphing library plotly.js.
install.packages("plotly")
  • Graphs created with the plotly R package are interactive!
fig <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
fig

2.10 stringr

  • The easiest way to get stringr is to install the whole tidyverse:
install.packages("tidyverse")
  • Alternatively, install just stringr:
install.packages("stringr")

2.11 patchwork

2.12 magrittr

2.13 data.table

2.14 cowplot

3 Data Visualization

3.1 tidyverse

3.3 viridis

3.4 tmap

3.5 leaflet

3.6 mapview

3.8 ggmap

3.9 rayshader

3.10 geojsonio

3.11 shinyjs

3.12 GGally

3.13 gridExtra

3.14 cartogram

3.15 visNetwork

4 Econometrics

4.1 AER

  • Applied Econometrics with R package installation:
install.packages('AER')

4.2 car

  • Companion to Applied Regression package installation:
install.packages('car')

4.3 censReg

  • Censored regression/tobit models package installation:
install.packages('censReg')

4.4 dummies

  • Automatically generating dummy/indicator variables

4.5 dynlm

  • Dynamic linear regression for time series

4.6 effects

  • Graphical and tabular illustration of partial effects

4.7 ggplot2

  • Advanced and powerful graphics

4.8 knitr

  • Combine R and IATEX code in one document

4.9 Imtest

  • Testing Linear Regression Models- Includes many useful tests for the linear regression model

4.10 maps

  • Draw geographical maps

4.11 MatchIt

  • Propensity Score Matching

4.12 mfx

  • Marginal effects, odds ratios and incidence rate ratios for GLMs

4.13 orcutt

  • Cochrane-Orcutt estimator for serially correlated errors

4.14 plm

  • Linear Models for Panel Data- A large collection of panel data methods

4.15 quantmod

  • Quantitative Financial Modelling

4.16 quantreg

  • Quantile regression, especially least absolute deviation (LAD) regression

4.17 rio

  • Conveniently import and export data files

4.18 sampleSelection

  • Sample selection models

4.19 sandwich

  • Different “robust” covariance matrix estimators

4.20 survival

  • Survival analysis and censored regression models

4.21 systemfit

  • Estimation of simultaneous equations models

4.22 truncreg

  • Truncated Gaussian response models

4.23 tseries

  • Time series analysis and computational finance

4.24 urca

  • Unit root and cointegration tests for time series data

4.25 vars

  • Structural vector autoregressive and error correction models

4.26 xtable

  • Export tables to LaTeX or HTML

4.27 xts

  • eXtensible Time Series- Irregular time series

4.28 WDI

  • Search, extract, and format data from the World Bank’s World Development Indicators

4.29 wooldridge

  • Data sets from the textbook of Wooldridge (2019)

4.30 zoo

  • Zeileis’ Ordered Observations- Irregular time series

5 GIS Data Manipulation

5.1 sf

5.2 raster

5.4 ncdf4

5.5 sp

6 Spatial Analysis

6.1 spatial

6.2 spatstat

6.3 gstat

6.4 spdep

6.5 geoshape

6.6 ggspatial

6.7 geogrid

6.8 GISTools

7 Spatial Data

7.1 OpenStreetMap

7.2 rnaturalearth

7.3 rnaturalearthdata

7.4 maps

7.5 spData

7.6 rgeos

7.7 jpndistrict

7.8 usmap

7.9 tigris

7.10 tidycensus

8 Text Mining

8.1 tidytext

8.2 tm

9 Bibliometric Analysis

9.1 bibliometrix

  • Install stable version
install.packages("bibliometrix")
  • Install beta version
install.packages("remotes")         
remotes::install_github("massimoaria/bibliometrix")

Updated on: 2025-02-13