Chapter 1 R

1.1 Basic online books

Books from Rstudio:

Book Link
R for Data Science
Advanced R
ggplot2: Elegant Graphics for Data Analysis
R Packages: Organize, Test, Document, and Share Your Code
bookdown: Authoring Books and Technical Documents with R Markdown
Tidy Modeling with R
Mastering Shiny
Spatial Data Science with R and “terra”

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:

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.