采采流水

An Essay Concerning Human Understanding

Notes about useful packages and functions in R Language

[Qiangua] / 2020-03-29


In this post, I will record some common R’s packages and functions.

1 Packages

1.1 ggplot2

1.2 data.table

1.3 tidyverse

1.4 magritter

1.5 patchwork

1.6 fs

1.7 rvest

1.8 purr

1.9 drake

  1. drake_plan(): create a workflow data frame
  2. make(): build your project
  3. vis_drake_graph(): show an interactive visual network representation of your workflow
  4. drake_history(): show what you built, when you built in, and the function arguments you used
  5. loadd(): load one or more built targets into your R session
  6. readd(): read and return a built target
  7. outdated(): see which targets will be built in the next make()
  8. deps(): check the dependencies of a command or function
  9. failed(): list the targets that failed to build in the last make()
  10. diagnose(): return the full context of a build, including errors, warnings, and messages

1.10 janitor

1.11 httr

Use GET() function to connect with website and get data from it.

1.12 V8

2 Tips

2.1 never use these two commands in the head of R script

setwd("**/**/**")
rm(list = ls())

Don’t try to change the .Rprofile and affect the data

2.2 check the loaded packages

(.packages())

2.3 set the working directory

setwd()

I custom the shortcuting in R with *Cmd+Shift+H*.

2.4 tidy code’s formatation

Here I just introduce the tidy method in RStudio.

  1. select the region, e.g. *Cmd + A*
  2. print shortcuting *Cmd + Shift + A* to format the code or run *style active file* in addin

2.5 get file’s path

full_path <- normalizePath(file_eg)
dir_name<-dirname()
file_name<-basename()

2.6 insert and run R chunck in Rmarkdown

insert: *Cmd+Opt+i* run: *Cmd+Shift+enter*

3 Run R Code

\[f=ma\]

library("ggplot2")
ggplot(mpg) + geom_point(aes(x = displ, y = hwy, colour = cty, size = cty))