R

Install R packages on an offline server

Prerequisite: Access to a machine with internet connections in the same operating system as the server’s operating system. If using RStudio on Windows, you need a Windows machine If using R in Linux, need a Linux machine (Farm server works too) Virtual machine and WSL should probably work Steps Install the same R version on your machine as the offline machine’s Windows will require Rtools to compile from source Create and open an R session in a new directory Install packages needed When installing some packages in an old version of R (e.

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) Combine plots Helpful packages: 3D rendering 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 Font format (e.

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.

How to split a string column by length

Intro This is a documentation of how I split a string type column by its length, and combine them together in a directory format (which was a necessary step for me to check whether each directory existed in my analysis). library(tidyverse) data <- tibble(string = c("123456", "987654")) print(data) ## # A tibble: 2 x 1 ## string ## <chr> ## 1 123456 ## 2 987654 Step 1 strsplit splits the string into a list of strings, and in tibble it will show up as a column of list type.

Tips for publishing your first document with bookdown and RStudio

I have just published my first document with bookdown and I am feeling euphoric at the moment. Somehow this gives me a very different satisfaction as compared to creating just a google document. Yes, they serve almost the same function, but the presentation of a bookdown document is much better. It also provides more functionalities and flexibilities than a google doc. Here are the minimal steps needed to customize a document from Yihui’s guide.

A R package that creates blank Rmd files

I have created this package to create a blank Rmd file by saving a nearly blank Rmd template to the menu when you create Rmd files. I think it at least follows the first half of the UNIX orthodox: programs that do one thing and do it well. 😂 Reflection: the main purpose for me to create this package, other than making life easier, is to practise how to publish a R package onto github and to install it from there.