library(praise.me)
praise_me()
You are astonishing!
praise_me()
You are delightful!
praise_someone("Patrick")
Patrick is extraordinary!
praise_someone("Harriet")
Harriet is delightful!
Lecturer: Emi Tanaka
Department of Econometrics and Business Statistics
Thanks to Stuart Lee for developing the initial content in this slide, which has been subsequently modified a fair amount by me.
praise.me
Documentation is vital
praise.me
packageThis package is for teaching demo only
praise.me
package is to give you or someone else a random word of praise.praise_me()
functionpraise_someone()
functionpraise_someone <- function(who = NULL) {
praises <- c(
"exceptional",
"remarkable",
"extraordinary",
"delightful",
"wonderful",
"fantastic",
"phenomenal",
"brilliant",
"astonishing",
"splendid"
)
affirmation <- sample(praises, 1)
ifelse(is.null(who),
paste0(tools::toTitleCase(affirmation), "!"),
paste0(who, " is ", affirmation, "!")
)
}
data-raw/praises.R
Or put code for praises
in a file under R/
if not using as exported data.
R/praise.R
#' @export
praise_me <- function() {
affirmation <- sample(praises$words, 1)
paste0("You are ", affirmation, "!")
}
#' @export
praise_someone <- function(who = NULL) {
affirmation <- sample(praises$words, 1)
ifelse(is.null(who),
paste0(tools::toTitleCase(affirmation), "!"),
paste0(who, " is ", affirmation, "!")
)
}
print
methodprint
is an S3 method and above is actually using print.default()
print
method for class praise
:R/praise.R
#' @export
praise_me <- function() {
affirmation <- sample(praises$words, 1)
out <- paste0("You are ", affirmation, "!")
praise_now(out)
}
#' @export
praise_someone <- function(who = NULL) {
affirmation <- sample(praises$words, 1)
out <- ifelse(is.null(who),
paste0(tools::toTitleCase(affirmation), "!"),
paste0(who, " is ", affirmation, "!")
)
praise_now(out)
}
praise_now <- function(praise) {
structure(praise, class = c("praise", "character"))
}
#' @export
print.praise <- function(x, ...) {
cat(x, ...)
}
%>%
operator in your package, you can import the magrittr
package (used to import pipe operator in all tidyverse
packages).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/praise.R
#' Praises you or someone
#'
#' @description
#' Praises you or someone with a random word.
#'
#' @param who A character of who to praise.
#'
#' @return An object of class `praise` and `character`.
#'
#' @examples
#' praise_me()
#' praise_someone()
#' praise_someone("Joanna")
#'
#' @export
praise_me <- function() {
affirmation <- sample(praises$words, 1)
out <- paste0("You are ", affirmation, "!")
praise_now(out)
}
#' @rdname praise_me
#' @export
praise_someone <- function(who = NULL) {
affirmation <- sample(praises$words, 1)
out <- ifelse(is.null(who),
paste0(tools::toTitleCase(affirmation), "!"),
paste0(who, " is ", affirmation, "!")
)
praise_now(out)
}
praise_now <- function(praise) {
structure(praise, class = c("praise", "character"))
}
#' @export
print.praise <- function(x, ...) {
cat(x, ...)
}
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/praise.me-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()
cowsay
-----
Hello
------
\ ^__^
\ (oo)\ ________
(__)\ )\ /\
||------w|
|| ||
This adds a line in the DESCRIPTION
file:
Imports:
cowsay
pkg::fun()
.#' Praises you or someone
#'
#' @description
#' Praises you or someone with a random word.
#'
#' @param who A character of who to praise.
#' @param by A character to say the praise. See the full
#' list of character by `list_character()`.
#'
#' @return An object of class `cheer`, which is
#' just a character with special print method.
#'
#' @examples
#' praise_me()
#' praise_me(by = "cow")
#' praise_someone()
#' praise_someone("Joanna", by = "cat")
#'
#' @export
praise_me <- function(by = NULL) {
affirmation <- sample(praises$words, 1)
praise_text <- paste0("You are ", affirmation, "!")
praise_now(praise_text, by = by)
}
#' @rdname praise_me
#' @export
praise_someone <- function(who = NULL, by = NULL) {
affirmation <- sample(praises$words, 1)
praise_text <- ifelse(is.null(who),
paste0(tools::toTitleCase(affirmation), "!"),
paste0(who, " is ", affirmation, "!")
)
praise_now(praise_text, by = by)
}
praise_now <- function(praise, by = NULL) {
if (is.null(by)) {
out <- praise
} else {
out <- cowsay::say(praise, by = by, type = "string")
}
structure(out, class = c("praise", "character"))
}
#' @export
print.praise <- function(x, ...) {
cat(x, ...)
}
#' @importFrom pkg fun
to drop the pkg::
.#' @import pkg
to import all functions in pkg
testthat
testthat
tests/testthat/test-praise.R
or for existing repo, run from the terminal:
praise.me
packagehttps://github.com/emitanaka/praise.me
.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
usethis::use_test() # make test file for active R file
# write some test
devtools::test_active_file() # test active file
devtools::test() # test whole package
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
ETC5523 Week 9