Creating and Publishing the text2numR Package: Step-by-Step Guide

I recently tried creating a simple R package—text2numR—with the help of ChatGPT. The process was surprisingly smooth and educational, especially for someone new to R package development. You can check out the finished project here: hasindu-k/text2numR.

Let’s walk through how it was done, step by step:


 

🔧 Step 1: Start a New Package Project

  1. Open RStudio

  2. Navigate to: File > New Project > New Directory > R Package

  3. Choose a name for your package (e.g., text2numR)

  4. Click "Create Project"

🖋️ Step 2: Add Your Functions

Create your R function files inside the /R folder:

encode_text.R

#' Encode a string into numeric positions
#'
#' @param text A character string
#' @return A numeric vector
#' @export
encode_text <- function(text) {
  chars <- strsplit(tolower(text), "")[[1]]
  match(chars, letters)
}

decode_text.R

#' Decode numeric positions into a string
#'
#' @param nums A numeric vector
#' @return A character string
#' @export
decode_text <- function(nums) {
  if (!is.numeric(nums)) {
    stop("Input must be a numeric vector.")
  }
  valid <- ifelse(nums >= 1 & nums <= 26, nums, NA)
  output <- ifelse(is.na(valid), "?", letters[valid])
  paste0(output, collapse = "")
}

📄 Step 3: Document and Build Your Package

Use roxygen2 for inline documentation, then run:

devtools::document()
devtools::build()

🚀 Step 4: Connect to GitHub

  1. Go to: Tools > Version Control > Project Setup

  2. Initialize Git (if not already done)

  3. Then in the console:

usethis::use_github()

🚀 Step 5: Add GitHub Actions for R CMD Check

In the console, run:

usethis::use_github_action_check_standard()

This creates .github/workflows/R-CMD-check.yaml to automate package checks.

📝 Step 6: Add a LICENSE File

Use the MIT license via:

usethis::use_mit_license("Hasindu Koshitha")

✅ Step 7: CRAN-Prep Checklist

CheckCommand
Spellingspelling::spell_check_package()
Unit Testsdevtools::test()
Local R CMD Checkdevtools::check()
Windows R-devel Checkdevtools::check_win_devel()
Build PDF Manualdevtools::build_manual()
Add NEWS.mdusethis::use_news_md()

🚀 What text2numR Does

  • Converts alphabetic strings into numeric sequences (e.g., "cab" → 3 1 2)

  • Converts numeric vectors back into text (e.g., 3 1 2 → "cab")

  • Supports:

    • Case insensitivity

    • Error handling (non-letter characters)

    • Batch processing

Ready to go live on GitHub and optionally submit to CRAN!

Comments

Popular posts from this blog

Use the Google Custom Search JSON API to retrieve images

Terminal commands relared to files in the Ubuntu

Laravel Best Practices: Building Robust and Maintainable Applications