Lecturer: Michael Lydeamore
Department of Econometrics and Business Statistics
Aim
Why
Documentation is vital
random_number()
functiondata-raw/census-birthplace.R
:
The base pipe (|>
) is fine for 99% of cases - I recommend just using that.
If you need the magrittr
pipe (%>%
) then you can use
The process to include a Shiny app is a bit more complicated than data or a function.
Put your app code in inst/[appname]/app.R
In your function to launch your app:
Files in inst/
are installed with your package but are considered “internal”.
The system.file
tells R where to look for your shiny app code.
Warning
Don’t include an example for running a shiny app! It will cause R to hang.
roxygen2
#'
above a function to write documentation for that functionroxygen2
uses @
tags to structure documentation, e.g.
@description
is the description@param
describes the arguments of the function@export
signals that it is an exported function@return
describes the return objectdevtools::document()
converts the Rd tags to appropriate sections of .Rd
files written in the man/
folderpraise.me
packageR/random_number.R
#' Histograms of random numbers
#'
#' Generates 100 uniform random numbers between 0 and `range`, and
#' plots a `ggplot2` histogram.
#'
#' You can modify the plot using
#' ```r
#' random_number(5) + ggplot2::theme_bw()
#' ```
#'
#' @param range **Maximum** value for the uniform distribution
#'
#' @return `ggplot2` histogram of the random values
#' @export
#'
#' @importFrom ggplot2 ggplot aes geom_histogram
#'
#' @examples
#' random_number(12)
random_number <- function(range) {
random_numbers <- data.frame(numbers = runif(n = 100, min = 0, max = range-1))
ggplot2::ggplot(data = random_numbers, aes(x = numbers)) +
geom_histogram()
}
usethis::use_data_raw()
to store R code to process raw data,usethis::use_data()
to save a binary file in data/
directory,praises
.data.R
or name-of-data.R
R/data.R
#' Number of Australian residents by country of birth from the 2016 and 2021 census
#'
#' @format ## 'census_birthplace'
#' A data frame with 105 rows and 4 columns
#' \describe{
#' \item{birth}{Country of birth}
#' \item{count}{Number of residents from that birth country}
#' \item{percentage}{Percentage of residents from that birth country}
#' \item{census}{Year of census (either 2016 or 2021)}
#' }
#' @source <https://www.abs.gov.au/websitedbs/censushome.nsf/home/2016>
"census_birthplace"
R/ausbirthplace-package.R
Depends
: Specify the version of R that the package will work with or package that it is dependent on (e.g. for ggplot2 extension packages, it depends on ggplot2).Imports
: External packages that are imported to use in your package. Most external packages are in this category.Suggests
: Packages that are not strictly needed but are nice to have, i.e. you use them in examples or vignettes.usethis::use_package()
or for existing repo, run from the terminal:
praise.me
packagehttps://github.com/MikeLydeamore/ausbirthplace
.README
file with installation instructions – this is displayed in the GitHub repo.README.Rmd
file withpkgdown
pkgdown
pkgdown::build_site()
_pkgdown.yml
file
available::available("pkgname") # check if package name is available (if planning to publish publicly)
usethis::create_package("pkgname")
usethis::use_git() # set up version control
usethis::use_github() # optional
usethis::use_r("myfile")
# write some functions in a script
usethis::use_data_raw() # if adding data
devtools::load_all() # try it out in the console
usethis::use_package("import-pkgname") # add package to import (or depends or suggests)
usethis::use_package_doc() # add package documentation
usethis::use_pipe() # if you want to add %>% from `magrittr`
usethis::use_vignette("vignette-name") # add vignette
devtools::build() # build vignettes
devtools::install() # to install package
devtools::check() # to build and check a package
usethis::use_readme_rmd() # to add a README Rmd file
styler::style_pkg() # optional (commit first, then review changes carefully)
usethis::use_pkgdown_github_pages() # for setting up pkgdown website on github
# `usethis::use_pkgdown()` if not using github pages
Summary
usethis
, devtools
, roxygen2
, and pkgdown
.ETC5523 Week 10