Setting Up R

Forewords

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. The following figure from that post is especially relavant to the current post since it explains how R looks for various kinds of configuration files:

Figure 1

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.

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/<version>/quarto-<version>-linux-amd64.deb && sudo apt install ./qurto.deb # required by `quarto`; substitute `<version>` for a specific version number
sudo quarto tools install tinytex # required by `quarto` for PDF outputs
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 recursive dependency of `embed`

Then, install the packages in R:

install.packages(
  c(
    "tidymodels", "xgboost", "lightgbm", "randomForest", "earth", "e1071", "embed", "themis", "textrecipes", "torch", "brulee", "luz", "tabnet", # machine learning
    "tsibble", "fable", "feasts", "fabletools", # time series
    "quarto", "blogdown", "pagedown", # publishing
    "gt", "gganimate", "ggwordcloud", "ggraph", # data visualization
    "odbc", "DBI", "RPostgres", "RMariaDB", # database related
    "miniUI", "datapasta", "markdown", # RStudio addins
    "reticulate", # python interface
    "data.table", "dtplyr", "slider", "httr2", "caTools", "bitops", "remotes" # misc
  )
)

torch::install_torch()

R setups

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

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

Edit ~/.Renviron and add the following environment variables for R:

LANGUAGE=en
LC_ALL=en_US.utf8

Edit ~/.Rprofile set the following options for R:

options(
  repos = "https://cloud.r-project.org/" # or "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"
)

Edit the .Rprofile file in the working directory of a blog project created with blogdown and set the following options:

options(
  blogdown.author = "zenggyu",
  blogdown.ext = ".Rmd",
  servr.daemon = TRUE
)

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 -> General:

  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.

Related

Next
Previous
comments powered by Disqus