11 Other tips for Efficient coding in R

11.1 Variable names !

Variable names need to help you every time you see them!

Examples:

11.1.1 Code formating

Install ‘slyler’ package to have RStudio addin to automate formatting your code

11.2 Code starter tricks

11.2.1 Most important / useful libraries

These are libraries / codes that one could use by default when starting a new code.

# 0. General libraries and functions ----

# options(digits = 3); # 7
# options(max.print = 100) # 1000
# options(scipen=999); #remove scientific notation

library(magrittr)
# library(readxl)
# library(readr)
# library(stringr)

## library(tidyverse) # includes:  readr ggplot2 stringr (which we use), dplyr tidyr tibble forcats purrr (may use later) and others - https://www.tidyverse.org/packages/

# library(dplyr) # or
#library(dtplyr)

# library(ggplot2) 
# library(lubridate,  quietly=T) # to work work times and dates
# options(lubridate.week.start =  1)

library(data.table) # needs to be AFTER lubridate
options(datatable.print.class=TRUE)

# library(dygraphs)
# library(plotly)
# library(DT)

`%ni%` = Negate(`%in%`)

11.3 Efficient workflows

11.3.1 Workflow: Data-first approach

So, you found a new Open Data dataset. Great - now you can do something with it! This is how you start .

11.3.2 Workflow: Task/needs/algorithm-first approach

TBA

11.4 Object oriented programming in R

https://techvidvan.com/tutorials/r-object-oriented-programming/

11.4.1 S3

11.4.2 R6

11.5 RStudio tricks

11.5.1 Coding online

Platforms for coding R online
https://rstudio.cloud/ (login required) - All in One place for learning and coding

11.5.1.1 Running multiple RStudio versions

You want to be able to run multiple versions of RStudio in Windows? You can do with the following executable .bat script.

# Run-RStudio-1.4.bat

@echo off
title Starting RStudio!
echo Hello Dear,
echo Starting RStudio 1.4 (from C:\Users\gxd006\Downloads\RStudio-1.4.1106\bin\) for you...

C:\Users\abc123\Downloads\RStudio-1.4.1106\bin\rstudio.exe

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Users\gxd006\Downloads" & chr(34), 0
Set WshShell = Nothing