If you already have a book set up like above, follow these steps to convert it to a package:
Package Infrastructure
Run the following code to create some of the package infrastructure. You can do everything manually, but I like to use the extremely helpful package usethis
.
# create a package DESCRIPTION file
usethis::use_description(check_name = FALSE)
# add license (change psyTeachR to main author)
usethis::use_ccby_license("psyTeachR")
# create the directory where the local copy of the book will go
dir.create("inst/book", recursive = TRUE)
# ignore book files when building the package
usethis::use_build_ignore("_bookdown_files")
usethis::use_build_ignore("files")
usethis::use_build_ignore("include")
usethis::use_build_ignore("images")
usethis::use_build_ignore("^.*\\.Rproj$", escape = FALSE)
usethis::use_build_ignore("^.*\\.Rmd$", escape = FALSE)
usethis::use_build_ignore("^.*\\.yml$", escape = FALSE)
usethis::use_build_ignore("^.*\\.bib$", escape = FALSE)
usethis::use_build_ignore("^.*\\.tex$", escape = FALSE)
usethis::use_build_ignore("^.*\\.rds$", escape = FALSE)
Add packages
Add any CRAN packages you want included with your package.
usethis::use_package("dplyr")
usethis::use_package("tidyr")
usethis::use_package("ggplot2")
# usethis::use_package("tidyverse", type = "depends")
If you want to add tidyverse, you need to add it with type = "depends"
and it will load automatically when you load this package. However, this can cause a lot of problems when installing the package, so I would not recommend it.
Add datasets
To add datasets, add a data_raw
directory and edit the file DATASET.R
to prep your datasets. Here's an example:
usethis::use_data_raw()
## code to prepare `DATASET` dataset goes here
factorial_2w2b <- faux::sim_design(
within = list(time = c(am = "Day", pm = "Night")),
between = list(pet = c(dog = "Dog Owner", cat = "Cat Owner")),
n = 25, mu = list(am = c(10, 12), pm = c(12, 14)),
sd = 5, r = 0.5, plot = FALSE)
usethis::use_data(factorial_2w2b, overwrite = TRUE)
You can set up the documentation by hand following the instructions at https://r-pkgs.org/data.html or use the following script to set up datasets from .csv or .xls files.
# function for creating dataset descriptions in Roxygen
make_dataset <- function(dataname, title, desc, itemdesc = list(), filetype = "csv", source = "", write = TRUE) {
# read data and save to data directory
datafile <- paste0("data-raw/", dataname, ".", filetype)
if (filetype == "csv") {
data <- readr::read_csv(datafile, col_types = readr::cols())
} else if (filetype == "xls") {
data <- readxl::read_xls(datafile)
}
# this is awkward, but devtools::document won't work unless the saved data has the name you intend to use for it
dat <- list()
dat[[dataname]] <- data
list2env(dat, envir = environment())
e <- paste0("usethis::use_data(", dataname, ", overwrite = TRUE)")
eval(parse(text = e))
# create Roxygen description
items <- paste0("#' \\item{", names(itemdesc), "}{", itemdesc, "}")
s <- sprintf("# %s ----\n#' %s\n#'\n#' %s\n#'\n#' @format A data frame with %d rows and %d variables:\n#' \\describe{\n%s\n#' }\n#' @source \\url{%s}\n\"%s\"\n\n",
dataname, title,
gsub("\n+", "\n#'\n#' ", desc),
nrow(data), ncol(data),
paste(items, collapse = "\n"),
source, dataname
)
if (!isFALSE(write)) write(s, paste0("R/", dataname, ".R"))
invisible(s)
}
After you set up the function above at the top of your DATASET.R
file, you can add datasets as below (either from files you've added to data-raw
or by creating and saving CSV files (e.g., using faux).
# add country codes dataset
ccodes <- read_csv("https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv")
write_csv(ccodes, "data-raw/country_codes.csv")
itemdesc <- list(
"name" = "Full country name",
"alpha-2" = "2-character country code",
"alpha-3" = "3-character country code",
"country-code" = "3-digit country code",
"iso_3166-2" = "ISO code",
"region" = "World region",
"sub-region" = "Sub-region",
"intermediate-region" = "Intermediate region",
"region-code" = "World region code",
"sub-region-code" = "Sub-region code",
"intermediate-region-code" = "Intermediate region code"
)
make_dataset("country_codes",
"Country Codes",
"Multiple country, subregion, and region codes for 249 countries.\nFrom https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes", itemdesc, source = "https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv")
This will create a new file in the R
directory called country_codes.R
that contains your dataset description. Add it to the package documentation by running devtools::document()
.
Add Shiny Apps
You can add shiny apps to you package easily. Just put any app directories in inst/apps
and add the following code to a new file R/app.R
(replacing "YOUR.PACKAGE.NAME").
#' Launch Shiny App
#'
#' @param name The name of the app to run
#' @param ... arguments to pass to shiny::runApp
#'
#' @export
#'
app <- function(name, ...) {
appDir <- system.file(paste0("apps/", name), package = "YOUR.PACKAGE.NAME")
if (appDir == "") stop("The shiny app ", name, " does not exist")
shiny::runApp(appDir, ...)
}
Make sure you add shiny and any other packages used in your shiny app as dependencies. Document to add the app()
function and install the updated package.
usethis::use_package("shiny")
usethis::use_package("shinydashboard")
devtools::document()
devtools::install()