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
n_A = dplyr::n(A)
the number oflast(A)
the end value of vector
1.4 Function
- Get the function name in inside of function:
as.character(dplyr::substitute(f_name))
1.5 Package
1.5.1 Metadata
- Inheriting parameters from other functions
#' @param a This is the first argument
<- function(a) a + 10
foo
#' @param b This is the second argument
#' @inheritParams foo
<- function(a, b) {
bar foo(a) * 10
}
- Documenting multiple functions in the same file
@describeIn xxx
or @rdname xxx
#' Foo bar generic
#'
#' @param x Object to foo.
<- function(x) UseMethod("foobar")
foobar
#' @describeIn foobar Difference between the mean and the median
<- function(x) abs(mean(x) - median(x))
foobar.numeric
#' @describeIn foobar First and last values pasted together in a string.
<- function(x) paste0(x[1], "-", x[length(x)]) foobar.character
#' Basic arithmetic
#'
#' @param x,y numeric vectors.
<- function(x, y) x + y
add
#' @rdname add
<- function(x, y) x * y times
1.5.2 Load Data
- put the data in Package
::use_data(xts_Q, xts_P, xts_T, xts_PET) usethis
- set the metadata for data
#' Steamflow
#' @format [xts] from 1999-01-01 to 2008-12-31 for two stations.
"xts_Q"