# adapted from devtools
# create_pkg(path = "~/github/ropenscilabs/fart")
create_pkg <- function(path, http_lib = "crul") {
check_package_name(path)
parent_dir <- normalizePath(dirname(path), winslash = "/", mustWork = FALSE)
if (!file.exists(parent_dir)) {
stop("Parent directory '", parent_dir, "' does not exist",
call. = FALSE)
}
if (!file.exists(path)) {
if (!dir.create(path)) {
stop("Failed to create package directory '", basename(path),
"'", call. = FALSE)
}
}
files <- list.files(path)
if (length(files)) {
valid <- length(files) == 1 && tools::file_ext(files) ==
"Rproj"
if (!valid)
stop("Directory exists and is not empty", call. = FALSE)
}
path <- normalizePath(path, winslash = "/", mustWork = TRUE)
setup(path, http_lib)
invisible(TRUE)
}
check_package_name <- function(path) {
name <- get_name(path)
if (!grepl("^[[:alpha:]][[:alnum:].]+$", name) && !grepl("\\.$", name)) {
stop(name, " is not a valid package name: it should contain only\n",
"ASCII letters, numbers and dot, have at least two characters\n",
"and start with a letter and not end in a dot.",
call. = FALSE)
}
}
get_name <- function(path) basename(normalizePath(path, mustWork = FALSE))
setup <- function(path = ".", http_lib = "crul") {
check_package_name(path)
parent_dir <- normalizePath(dirname(path), winslash = "/", mustWork = TRUE)
message("Creating package '", get_name(path), "' in '", parent_dir, "'")
dir.create(file.path(path, "R"), showWarnings = FALSE)
create_desc(path, http_lib)
create_namespace(path)
create_lisc(path)
create_pkg_level_man(path, http_lib)
create_utils(path)
invisible(TRUE)
}
create_namespace <- function(path) {
ns_path <- file.path(path, "NAMESPACE")
if (file.exists(ns_path))
return()
cat("# Generated by roxygen2: fake comment so roxygen2 overwrites silently.\n",
"exportPattern(\"^[^\\\\.]\")\n", sep = "", file = ns_path)
}
create_desc <- function(path, http_lib = "crul") {
txt <- sprintf('Package: %s
Title: What the Package Does (one line, title case)
Version: 0.0.1
Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
License: MIT + file LICENSE
LazyData: true
Imports:
%s,
jsonlite,\n glue\n', get_name(path), http_lib)
cat(txt, file = file.path(path, "DESCRIPTION"))
}
create_lisc <- function(path) {
txt <- sprintf('YEAR: %s
COPYRIGHT HOLDER: First Last\n', strextract(Sys.Date(), "[0-9]{4}"))
cat(txt, file = file.path(path, "LICENSE"))
}
create_pkg_level_man <- function(path, http_lib = "crul") {
http_lib_imports <- switch(http_lib,
crul = "@importFrom crul HttpClient",
httr = "@importFrom httr VERB stop_for_status content"
)
name <- get_name(path)
txt <- sprintf("#\' PACKAGE DESCRIPTION
#\' %s
#\' @importFrom jsonlite fromJSON
#\' @importFrom glue glue
#\' @name %s-package
#\' @aliases %s
#\' @docType package
NULL\n", http_lib_imports, name, name)
txt <- paste0(
"# Auto-Generated by `apipkgen` package:\n# See apipkgen docs for help\n\n",
txt)
cat(txt, file = file.path(path, sprintf("R/%s-package.R", name)))
}
create_utils <- function(path) {
txt <- 'ct <- function(l) Filter(Negate(is.null), l)'
cat(paste0(auto_gen, txt), file = file.path(path, "R/zzz.R"))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.