Setting Up R

hacking skills
Author

zenggyu

Published

2018-06-02

Modified

2023-06-17

Abstract
Guidance on setting up a fresh R installation.

Introduction

This is one of a series of posts where I document software configurations for personal reference. This post documents the configurations for R.

Notes on R’s startup process

There’s a good post from rstudio that explains R’s startup process. Figure 1 from that post is especially relevant to the current post since it explains how R looks for various kinds of configuration files:

Figure 1: R startup process

This post will concentrate on user- or project-specific files, so all the files mentioned below should be placed in a user’s home directory or in the working directory of a project.

Basic setups

Edit ~/.bashrc and create an alias for R with some useful startup options:

alias R='R --no-save --no-restore-data'

Edit ~/.Rprofile and add the following content:

options(
  repos = c(default = "https://cloud.r-project.org/") # or "https://mirrors.tuna.tsinghua.edu.cn/CRAN/", "https://mirrors.pku.edu.cn/CRAN/", "https://mirrors.sustech.edu.cn/CRAN/", etc.
)

if (interactive()) {
  suppressMessages(require(devtools))
}

R and package installation

To install base R and tidyverse, follow these instructions. To install other frequently used packages, follow the steps below.

First, install system dependencies in shell:

# system dependencies
wget -O ./quarto.deb https://github.com/quarto-dev/quarto-cli/releases/download/v<version>/quarto-<version>-linux-amd64.deb && sudo apt install ./quarto.deb # required by `quarto`; substitute `<version>` for a specific version number
sudo apt install libcurl4-openssl-dev libssl-dev libxml2-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev # required by tidyverse
sudo apt install xclip wl-clipboard # required by RStudio addin `clipr` (choose`xclip` for x11, or `wl-clipboard` for wayland)
sudo apt install unixodbc-dev # required by `odbc`
sudo apt install libpq-dev # required by `RPostgres`
sudo apt install libmariadb-dev # required by `RMariaDB`
sudo apt install libv8-dev # required by `V8`, which is a dependency of `embed`
sudo apt install libgsl-dev # required by `RcppGSL`, which is a dependency of `tidyclust`
sudo apt install libsodium-dev # required by plumber

Then, install the following packages and relevant dependencies in R:

install.packages(
  c(
    "tidyverse", "tidymodels", "devtools", # meta-package
    "randomForest", "xgboost", "lightgbm", "bonsai", # supervised learning: tree-based
    "torch", "luz", "brulee", "tabnet", # supervised learning: deep learning
    "tsibble", "fable", "feasts", "fabletools", # supervised learning: time series
    "censored", "tidyclust", # unsupervised learning
    "embed", "themis", "textrecipes", "stacks", "probably", # machine learning: pre/post-processing
    "earth", "e1071", # machine learning: other
    "corrr", # statistics
    "quarto", "tinytex", # technical writing
    "DT", "gt", "gganimate", "ggwordcloud", "ggraph", # data visualization
    "odbc", "DBI", "RPostgres", "RMariaDB", # database interface
    "miniUI", "datapasta", "markdown", # RStudio addins
    "reticulate", # python interface
    "httr2", "plumber", "shiny", # web programming
    "data.table", "dtplyr", "slider", "caTools", "bitops", "remotes", "bundle" # misc
  )
)

torch::install_torch()

tinytex::install_tinytex(repository = "http://mirrors.tuna.tsinghua.edu.cn/CTAN/", bundle = "TinyTeX") # restart RStudio if this command is executed in it
# alternatively, download a tinytex release from https://github.com/rstudio/tinytex-releases/releases/tag/daily and then execute:
# tinytex:::install_prebuilt('./TinyTeX.tar.gz') # restart RStudio if this command is executed in it

tinytex::tlmgr_repo('http://mirrors.tuna.tsinghua.edu.cn/CTAN/')
tinytex::tlmgr_install("xecjk")

File templates

See this post.

RStudio setups

Tools -> Global Options -> General:

  1. Uncheck “Restore .RData into workspace at startup”.
  2. Set “Save workspace to .RData on exit” to “Never”.
  3. Uncheck “Always save history (even when not saving .RData)”.

Tools -> Global Options -> Code -> Editing:

  1. Check “Use native pipe operator, |> (requires R 4.1+)”.
  2. Check “Soft-wrap R source files”.

Tools -> Global Options -> Appearance:

  1. Editor font: “Ubuntu Mono”.
  2. Editor font size: 12.
  3. Editor theme: “Solarized Light”.

Tools -> Global Options -> R Markdown:

  1. Uncheck “Show output inline for all R Markdown documents”

Tools -> Modify Keyboard Shortcuts:

  1. Redo -> ctrl+y (by default, this shortcut is occupied by ‘paste last yank’).
  2. Reindent Selection -> ctrl+shift+i.
  3. Reformat Current Selection -> ctrl+shift+f (by default, this shortcut is occupied by ‘find in files’).
  4. Show Help for Current Function -> ctrl + e.