Install R packages on an offline server

Use renv to automatically manage installation and dependencies

Big data in R

Reading files vroom Skip columns: col_types = list(hp = col_integer(), cyl = col_skip(), gear = col_factor())

Use reprex without browsers

The following code will generate a randomly named *_reprex.md file that can be copied for others. reprex::reprex( { library(tibble) library(purrr) library(dplyr) mutate(tibble(a=1),b = map(a, ~ tibble(c=1))) } , outfile = "reprex.html", html_preview=FALSE, session_info = TRUE )

Base R notes

Many functions in base R has faded away from my daily use of R because of tidyverse and the paradigm to do as many operations as possible in a data.frame. Get the variable name deparse(substitute(variable)) Indexing and subsetting which to return a logical vector that can be used in [] for subsetting Tidyverse alterantive (notes for myself) Imagine that I have a list of data.frames (group_split split a dataframe into lists of dataframes by the value of column specified)

Making tables in Rmarkdown: {DT} and {kableExtra}

{DT} is a package to render html tables. It is an interface to the datatables javascript library. It should not be confused with the {data.table} package, which is a package useful for data wrangling. A similar package of the same purpose is {kableExtra}. I found that {kableExtra} is more suitable for making static tables, whereas {DT} is more suitable for making interactive tables. DT Adding captions DT::datatable(iris[1:10,],caption = htmltools::tags$caption( style = 'caption-side: top; text-align: center; color:black; font-size:200% ;','Table1: Iris Dataset Table') )

Rmarkdown and markdown notes

This contains notes for Rmarkdown and markdown. All notes for markdown are generally applicable for Rmarkdown. markdown Footnote Rmarkdown Markdown extras Adding toggle <details><summary>toggle title</summary> toggle content </details> Image quality knitr::opts_chunk$set(dpi=300) Share html report A report should be self-contained. In xaringan, set self_cotained = TRUE in yaml and download html with Chrome, not Firefox. For DT, downloaded html from browser does not work - see here.

String processing in R

Case snakecase::to_any_case() help to fix the case of words - useful for converting between presentation data and processing data. Regex The regex in R is not 100% perl flavored. For example, escape character is \\ instead of \. I love the [{rev}](https://github.com/kevinushey/rex package). The most salient point that this package solves is on the interpretation of regex. When not to use regex Not all strings are interpreted as a regex. Sometimes, one needs to opt-in via a parameter in the function.

The bright side of plots (R plot notes)

Font Font size Font format (e.g. subscripts) Font family Combine plots Helpful packages: Colours 3D rendering Rough plots It will be useful to consult Rmarkdown notes because I often use Rmarkdown to render ggplot. Font Font size Global adjustment (e.g. the default font size is small when rendering with Rmarkdown with ) theme’s basic size See individual adjustment here and also in this RStudio2021 conference talk and this

Working with NA in R

NA are necessary markers for missing data. However, Working with them can be tricky because of their special properties. Care should also be taken when reading in and presenting the data. Properties of NA Types There are different types of NA that are denoted by the NA_*. This shhould be noted when working with NA data in a data.frame. Operations like case_when require all output data to be of the same type.

Learning functional programming in R

Why use functional programming Avoid intermediate objects In any loop, the standard practice is to create a new list before the loop, do some processing for each element of the list in the loop and then add the processing result as an element to the new list (following the same index). It makes programming more fun Thinking about that index i is simply not as fun as working with the whole list.