This comprehensive guide walks you through transforming reusable R functions into a professional-grade package, sharing it on GitHub, and maintaining it long-term. The process is organized into three phases:
Before beginning, ensure you have:
devtools
and usethis
packages:
install.packages(c("devtools", "usethis"))
roxygen2
package:
install.packages("roxygen2")
package_name <- "aucmat"
pack_path <- file.path("C:/Users/tralucck/OneDrive/", package_name)
your_name <- "Lucas VHH Tran"
# Create the package structure
usethis::create_package(pack_path)
This creates a new directory with the essential structure of an R
package. Open the newly created .Rproj
file in RStudio
immediately.
# Install required packages if needed
needs <- c("usethis", "devtools", "roxygen2", "testthat", "pkgdown",
"gitcreds", "git2r", "gert", "here")
to_install <- setdiff(needs, rownames(installed.packages()))
if (length(to_install)) install.packages(to_install)
# Check if Git is available
ok_git <- tryCatch({ nzchar(Sys.which("git")) }, error = function(e) FALSE)
if (!ok_git) message("Git not found on PATH. Install from https://git-scm.com/ and restart R.")
# Set up GitHub Personal Access Token if not already done
if (is.na(Sys.getenv("GITHUB_PAT", unset = NA))) {
message("No GITHUB_PAT detected. Creating one...")
usethis::create_github_token()
gitcreds::set_github_pat()
}
# Configure Git
usethis::use_git_config(user.name = your_name, user.email = "tranhungydhcm@gmail.com")
# Create R files for your functions
list_of_functions <- c("utils", "plot_roc_with_combos",
"generate_data_analytical", "tableroc")
for (func in list_of_functions) {
usethis::use_r(func)
}
Example function in R/utils.R
:
#' Calculate AUC from ROC data
#'
#' @param sensitivity Numeric vector of sensitivity values
#' @param specificity Numeric vector of specificity values
#' @return Numeric value representing AUC
#' @examples
#' # Calculate AUC for example data
#' sens <- c(0, 0.5, 1)
#' spec <- c(1, 0.5, 0)
#' calculate_auc(sens, spec)
#' @export
calculate_auc <- function(sensitivity, specificity) {
# Implementation here
1 - sum(diff(1 - specificity) * sensitivity[-1])
}
Edit the README.md file to include:
Edit the DESCRIPTION
file to include accurate
metadata:
Package: aucmat
Title: A Toolbox for Biomarkers Data Analysis
Version: 0.0.0.9000
Date: 2025-09-18
Authors@R:
person("Lucas", "VHH Tran", email = "tranhungydhcm@gmail.com", role = c("aut", "cre"))
Description: This package provides functions to download, process, and analyze biomarkers data with a focus on ROC analysis and visualization.
License: MIT + file LICENSE
URL: https://github.com/vanhungtran/aucmat
BugReports: https://github.com/vanhungtran/aucmat/issues
Depends:
R (>= 4.0.0)
Imports:
dplyr,
ggplot2,
BiocManager,
GEOquery
Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0)
Config/testthat/edition: 3
VignetteBuilder: knitr
The development cycle consists of:
DESCRIPTION
devtools::document()
after changing roxygen2 commentsdevtools::test()
to ensure
nothing is brokendevtools::check()
to ensure
package health# Package website
usethis::use_pkgdown() # Add pkgdown config
pkgdown::build_site() # Build docs site in docs/
usethis::use_github_pages() # Publish via GitHub Pages
# Continuous integration
usethis::use_github_action_check_standard() # Set up CI
# Ignore unnecessary files
usethis::use_build_ignore(c("doc", "docs", "README_files"))
File | Purpose | Pro Tip |
---|---|---|
DESCRIPTION |
Metadata: name, version, author, description, dependencies | Be precise. List all Imports . Use
Authors@R with ORCID |
README.md |
The front page of your GitHub repository | Include a clear, compelling example that works |
LICENSE |
Legal terms for sharing your code | Never leave this blank.
use_mit_license() is a safe default |
NAMESPACE |
(Auto-generated) Lists exported and imported functions | Manage with @export and @importFrom
roxygen2 tags |
man/*.Rd |
(Auto-generated) Function help files | Write clear roxygen2 comments with @param and
@return |
NAMESPACE
/man pages: run
devtools::document()
use_package("pkg")
and
@importFrom pkg fun
, then document()
devtools::check()
output, fix
issues, and re-runquickstart <- function(pkg_path = "C:/Users/tralucck/OneDrive/aucmat",
pkg_title = "AUC Matrix Analysis",
author = "Lucas VHH Tran",
github_repo = NULL) {
# Create package
usethis::create_package(pkg_path)
# Set up basics
usethis::use_mit_license(author)
usethis::use_readme_md()
usethis::use_testthat()
usethis::use_vignette("introduction")
# Configure Git and GitHub
usethis::use_git_config(user.name = author, user.email = "tranhungydhcm@gmail.com")
if (!is.null(github_repo)) {
usethis::use_github(repo = github_repo)
}
message("Package ", basename(pkg_path), " created successfully!")
}
This guide provides a comprehensive workflow for creating, publishing, and maintaining R packages. By following these steps, you can transform your R code into professional, shareable packages that follow best practices for documentation, testing, and distribution.
Remember that package development is iterative—start simple and gradually add complexity as needed. The R package ecosystem provides excellent tools to support this process, making it easier than ever to create high-quality, reusable code. ```
I’ve updated the guide to move the “Create a Compelling README” step before the “Edit the DESCRIPTION File” step as requested. The steps are now in this order: