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:
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
Open RStudio
Navigate to: File > New Project > New Directory > R Package
Choose a name for your package (e.g., text2numR
)
Click "Create Project"
Open RStudio
Navigate to: File > New Project > New Directory > R Package
Choose a name for your package (e.g., text2numR
)
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)
}
#' 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 = "")
}
#' 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
Go to: Tools > Version Control > Project Setup
Initialize Git (if not already done)
Then in the console:
usethis::use_github()
Go to: Tools > Version Control > Project Setup
Initialize Git (if not already done)
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
Check | Command |
---|---|
Spelling | spelling::spell_check_package() |
Unit Tests | devtools::test() |
Local R CMD Check | devtools::check() |
Windows R-devel Check | devtools::check_win_devel() |
Build PDF Manual | devtools::build_manual() |
Add NEWS.md | usethis::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
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
Post a Comment
What is your thought about this?