Chapter 1 R

1.1 Basic online books

Books from Rstudio:

https://www.rstudio.com/resources/books/

Book Link
R for Data Science http://r4ds.had.co.nz/
Advanced R https://adv-r.hadley.nz/
ggplot2: Elegant Graphics for Data Analysis https://ggplot2-book.org/
R Packages: Organize, Test, Document, and Share Your Code https://r-pkgs.org/
bookdown: Authoring Books and Technical Documents with R Markdown https://bookdown.org/yihui/bookdown/
Tidy Modeling with R https://www.tmwr.org/
Mastering Shiny https://mastering-shiny.org/
Spatial Data Science with R and “terra” https://rspatial.org/terra/index.html

1.2 Packages

Package Utility
tidyverse Data processing
terra Spatial data
shiny Web application

1.3 Data index

  1. n_A = dplyr::n(A) the number of

  2. last(A) the end value of vector

1.4 Function

  1. Get the function name in inside of function:
as.character(dplyr::substitute(f_name))

1.5 Package

1.5.1 Metadata

  1. Inheriting parameters from other functions
#' @param a This is the first argument
foo <- function(a) a + 10

#' @param b This is the second argument
#' @inheritParams foo
bar <- function(a, b) {
  foo(a) * 10
}
  1. Documenting multiple functions in the same file

@describeIn xxx or @rdname xxx

#' Foo bar generic
#'
#' @param x Object to foo.
foobar <- function(x) UseMethod("foobar")

#' @describeIn foobar Difference between the mean and the median
foobar.numeric <- function(x) abs(mean(x) - median(x))

#' @describeIn foobar First and last values pasted together in a string.
foobar.character <- function(x) paste0(x[1], "-", x[length(x)])
#' Basic arithmetic
#'
#' @param x,y numeric vectors.
add <- function(x, y) x + y

#' @rdname add
times <- function(x, y) x * y

1.5.2 Load Data

  1. put the data in Package
usethis::use_data(xts_Q, xts_P, xts_T, xts_PET)
  1. set the metadata for data
#' Steamflow
#' @format [xts] from 1999-01-01 to 2008-12-31 for two stations.
"xts_Q"