R package development

In quantitative research, the automation of data processing and the generation of visualizations are central components of the analytical workflow. Within this context, developing reusable functions and organizing them into utility modules is particularly valuable.

This approach enhances efficiency by minimizing repetitive tasks and eliminating the need to repeatedly recreate commonly used routines. It also contributes to clearer, more maintainable, and reproducible code.

A natural extension of this practice is the consolidation of these utilities into a structured package. As the number of functions grows, managing them as isolated scripts becomes increasingly inefficient, whereas packaging provides a scalable and systematic framework for organization and reuse.

Within the R ecosystem, several tools facilitate the development and maintenance of custom packages, simplifying the process of structuring, documenting, and distributing these analytical utilities.

install.packages(c("devtools", "usethis", "testthat", "roxygen2"))

Some times the installation process will return an error associate to the following packages that you will have to update or install:

install.packages("promises")
update.packages(ask = FALSE)
 
install.packages("shiny")
install.packages("miniUI")
install.packages("devtools") 

 This is the process that I use with some notes, and I encourage you to fallow or improve and share your recommendations with me:

# //////////////////////////////////////////////////////////////////////////// #
# //////////////////////////////////////////////////////////////////////////// #
# //////////////////////////////////////////////////////////////////////////// #
# //////////////////////////////////////////////////////////////////////////// #

# ------------------------------------------------------------------------------
# CREAR UN PAQUETE DE R
# ------------------------------------------------------------------------------

install.packages(c("devtools", "usethis", "testthat", "roxygen2"))

library(devtools)
library(usethis)

# ------------------------------------------------------------------------------
# Crear la estructura base del paquete
create_package("./repo/package_name")

setwd("./repo/package_name")
getwd()

# Configurar tests (crea la estructura)
use_testthat()

# ------------------------------------------------------------------------------
# Crear archivo para funciones de R (R/line_fig.R)
use_r("line_fig")

# Escribir la función en R/line_fig.R con documentación roxygen2
# Ejemplo:
#
# #' Create a line plot
# #'
# #' @param x Numeric vector
# #' @param y Numeric vector
# #' @return A line plot
# #' @export
# line_fig <- function(x, y) {
#   plot(x, y, type = "l")
# }

# ------------------------------------------------------------------------------
# Crear archivo de test (genera un archivo tests/testthat/test-line_fig.R)
use_test("line_fig")

# Ilustracion de un test:
# test_that("line_fig runs without error", {
#   expect_no_error(line_fig(1:10, 1:10))
# })

# ------------------------------------------------------------------------------

# Generar documentación y NAMESPACE
document()

# Ejecutar pruebas
test()

# Revisión completa
check()

# Construir paquete (genera un archivo package_name_1.0.0.tar.gz)
build()

# Instalar paquete localmente
install()
# devtools::install() #desde la carpeta del paquete
# o:
# install.packages("package_name_1.0.0.tar.gz", repos = NULL, type = "source")


# ------------------------------------------------------------------------------
# ACTUALIZATION OF PACKAGES

setwd("C:/Users/fogando/Documents/GitHub/package_name")

library(devtools)

# editar funciones y tests
document()
test()
check()
build()

# detach("package:package_name", unload = TRUE)
install()
# update.packages("autoecoreportpk_1.0.tar.gz")

# ------------------------------------------------------------------------------

 

As a key note base in my experience, be carrefull with the special character en el .Rd file. Because could cause fail error in too many cases.

 This blog have a second part focus on the main principles and best practice in documentation

 

 

 


Entradas populares

SQL

Lo mas consultado

SQL

Entradas populares

SQL