# install devtools if you don't have it already
install.packages("devtools")
UpdateR and (re-)Install Packages
This script is to be run whenever you want to update R and/or install your required packages. Whenever you install a new package, try to remember to add it to this file. This way, whenver you update, you can just run this script and will then have all your required packages ready to go. Newer versions of RStudio automatically check for missing packages when opening a script, so forgetting to install CRAN packages will be less detrimental to your workflow.
Check for R update: updateR
Do you need to update R? If using a Mac, you can do this by using the updateR
package (some steps found here):
# install updateR
::install_github('andreacirilloac/updateR') devtools
# run; you will be prompted for admin password
::updateR() updateR
updateR
macOS compatibility
As of mid-2023, updateR()
spits out an error after entering your password:
~/.Rprofile
Updated :Error in if (!compactible) stop(sprintf(e, status$latest, macOS), call. = FALSE) :
Password argument is of length zero
This seems to have something to do with the newest Mac OS (see: discussion on the package GitHub). For now, manual update only directly from CRAN.
Check for R update on Windows: installR
The installR
package also has an updateR()
function which only works on Windows. You will need to run the function in directly in R, not RStudio!
# install installr
install.packages("installr")
# run; you will be prompted for admin password
::updateR() installr
Global options
My preferred global options (RStudio > Tools > Global options):
- General > Basic
- R Sessions
- uncheck ‘Restore previously open source documents at startup’
- Workspace (for reproducibile workflow!!!)
- uncheck ‘Restore .RData into workshapce at startup’
- Save workspace to .RData on exit: Never
- R Sessions
- Code > Display
- General
- Show whitespace characters
- Allow scroll past end of document
- highlight selected line
- General
- Appearance
- Editor theme: Cobalt
Install packages
Packages can be available on CRAN or through developer versions.
CRAN packages
First make a list of your packages available on CRAN.
# CRAN packages ####
# run THIS chunk after updating R/RStudio
# from 'install_packages_if_missing.R'; roughly alphabetical
<- c("binom", "bookdown", "broman", "citr", "dplyr", "doBy",
required_packages "beepr", # to play sounds
"emmeans", "EMAtools", "grid",
"ggplot2", "ggdark", "ggstatplot",
"ggpp", # grammar extension of ggplot2
"ggpubr",
"ggrain",
"formatR", "janitor",
"here", "knitr", "kableExtra", "lme4", "lmerTest",
"png", "pryr", "papaja", "performance",
"tidyverse", "Rmpfr", "rmarkdown", "rotations",
"quarto",
"starpolishr", "stargazer", "simr",
"xtable")
Then, run this list through a for-loop that checks whether you’ve got these packages installed already. If not, the package will be installed.
for (package in required_packages) {
print(paste0("checking for install of ", package))
if (!requireNamespace(package)) install.packages(package, repos = "http://cran.rstudio.com")
}
Install dev packages: devtools
and remotes
Now install the packages that aren’t on CRAN. First, install devtools
and remotes
if you don’t already have them installed.
# Install remotes package if necessary
if(!requireNamespace("devtools", quietly = TRUE)) install.packages("devtools")
# Install remotes package if necessary
if(!requireNamespace("remotes", quietly = TRUE)) install.packages("remotes")
ggrain
package
For raincloud plots geom
if (!require(remotes)) {
install.packages("remotes")
}::install_github('jorvlan/raincloudplots')
remotes
library(raincloudplots)
papaja
package
- APA formatted templates
# download developer papaja (code https://github.com/crsh/papaja)
# Install the stable development version from GitHub
::install_github("crsh/papaja")
remotes
# Install the latest development snapshot from GitHub
::install_github("crsh/papaja@devel") remotes
rbbt
package
- integrates Zotero with RStudio
# Install rbbt Addin from GitHub to use Zotero
::install_github("paleolimbot/rbbt") remotes
To set-up a citation entry shortcut: RStudio > Tools > Modify Keyboard Shortcuts > enter ‘Zotero’ in the search > choose ‘Insert Zotero Citations’ > add a shortcut (I like Ctrl+K)
brms
package
- for running Bayesian models
# brms packages ####
# From https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started
# run the next line if you already have rstan installed
# remove.packages(c("StanHeaders", "rstan"))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
cmdstanr
package
- for running Bayesian models
# and cmdstanr (https://mc-stan.org/cmdstanr/)
::install_github("stan-dev/cmdstanr")
remotes# or
# install cmdstanr
::install_cmdstan(cores = parallel::detectCores(), overwrite = TRUE) cmdstanr
bcogsci
package
- companion package for the textbook An Introduction to Bayesian Data Analysis for Cognitive Science
# run the nextline if you don't have 'devtools' already installed
# install.packages("devtools")
::install_github("bnicenboim/bcogsci") remotes
Optional developer packages
- these only need to be installed if specifically wanted
stargazer
package
- these steps were taken when updatin to R v.4.2.1 as a work around to a problem that cropped up; only run this chunk if you start having problems with stargazer
- as of March 14, 2023 it’s still needed
# 31.08.2022: updated to R version 4.2.1 (2022-06-23), now stargazer won't compile tables where model names are too long
# Solution: discussed here https://www.reddit.com/r/rstats/comments/ucmtdn/issue_with_stargazer_package_after_update_to_r_420/
# code below from linked solution https://gist.github.com/alexeyknorre/b0780836f4cec04d41a863a683f91b53
## Quick fix for stargazer <= 5.2.3 is.na() issue with long model names in R >= 4.2
# Unload stargazer if loaded
detach("package:stargazer",unload=T)
# Delete it
remove.packages("stargazer")
# Download the source
download.file("https://cran.r-project.org/src/contrib/stargazer_5.2.3.tar.gz", destfile = "stargazer_5.2.3.tar.gz")
# Unpack
untar("stargazer_5.2.3.tar.gz")
# Read the sourcefile with .inside.bracket fun
<- readLines("stargazer/R/stargazer-internal.R")
stargazer_src # Move the length check 5 lines up so it precedes is.na(.)
1990] <- stargazer_src[1995]
stargazer_src[1995] <- ""
stargazer_src[# Save back
writeLines(stargazer_src, con="stargazer/R/stargazer-internal.R")
# Compile and install the patched package
install.packages("stargazer", repos = NULL, type="source")
### FROM NOW ON: shorter model names for stargazer
PsyTeachR Introdataviz
- can produce violin plots and raincloud plots
# how to install the introdataviz package to get split and half violin plots
::install_github("psyteachr/introdataviz", dependencies = TRUE) remotes
starpolishr
package
- availabile on github
- post-polishing of stargazer tables
# install.packages("devtools")
::install_github("ChandlerLutz/starpolishr") devtools
CogSci paper template
- used to write CogSci conference proceedings papers
# Install CogSci paper template
::install_github("kemacdonald/cogsci2016") remotes
Loading packages
Moving forward, use the following code at the beginning of your scripts instead of the long list of library(package)
. This will also check whether packages are installed, if they are it’ll load them, and if they’re not it’ll install them and then load them.
# don't use scientific notation
options(scipen=999)
# copy this chunk at the beginning of new chapters; will automatically load packages and install needed packages
## First specify the packages of interest
<- c("here", "tidyverse", "dplyr", "formatR", "stringr",
packages "janitor", "dplyr", "ggplot2", "lmerTest", "stargazer",
"MASS", "afex", "knitr", "gridExtra", "grid",
"paletteer",
"remotes",
"Rmisc")
## Now load or install&load all
<- lapply(
package.check
packages,FUN = function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, dependencies = TRUE)
library(x, character.only = TRUE)
}
} )
Manual installs
tinyTex
- to render documents with LaTeX under the hood, run the following in the terminal:
quarto install tinytex
Citations with Zotero
To use the rbbt
package installed above, we need to have Zotero installed (and Better BibTex).
install Zotero
https://www.zotero.org/download/
Better BibTex
Install from the [website]{https://retorque.re/zotero-better-bibtex/installation/}
then in Zotero: Tools / Add-ins / Settings wheel / choose downloaded file / Restart Zotero
set your citation keys: Zotero > Settings > Better BibTex > Citation key formula > (I like “zotero.clean”)
- if you need to update the BibTex key in your docs:
- highlight all your files in the Zotero Library, right-click > Better BibTex > Refresh BibTex key
- if you need to update the BibTex key in your docs: