R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
250: Building an Internal Company R Package
I've sat through countless code reviews where a junior analyst says, "I've already written the helper functions for this project; I'll just email you the utils.R script and you can source() it at the top of your file."
The "source() script is good enough" fallacy
It feels efficient in the moment. You have a few functions that calculate Company-Specific Adjusted Revenue or clean up the weird naming conventions in your SQL database, and you want your teammates to use them. But source() is a trap.
Imagine you've shared company_utils.R with five people. Three months later, you realize there's a bug in the calculate_clv() function. You fix it in your local version. Now, you have to track down every single person who downloaded that script, tell them to delete the old one, and send them the new one. Worse, someone has probably already modified the script locally to "make it work" for their specific project, and now you have five different versions of the "truth" floating around the department.
# The "Old Way" (and why it fails)
source("C:/Users/jane/Documents/scripts/company_utils.R")
# What happens if Jane leaves the company?
# What happens if the file path is different on your machine?
# What happens if you don't know what arguments calculate_clv() takes?
Treating your internal tools like a first-class product
The moment a piece of code is used by more than one person, it stops being a "script" and starts being a "product." The correct way to handle this is by building an internal R package. I know, "package" sounds like something only Tidyverse developers do, but an internal package is really just a standardized way of bundling code, documentation, and dependencies.
When you wrap your logic in a package, you gain three immediate superpowers: Versioning, Dependency Management, and Discoverability. Instead of hunting through a folder for a script, your colleagues just type library(corpdata). If they forget how a function works, they type ?calculate_clv and see a professional help page instead of scrolling through 500 lines of code to find your comment.
Turning utility functions into a documented library
You'll want to use devtools and roxygen2. I strongly suggest you don't try to write the .Rd help files by hand—that's a waste of your time. Use roxygen comments directly above your functions.
Take this example. Instead of a loose function in a script, we put it in R/clv_metrics.R within our package structure:
#' Calculate Customer Lifetime Value
#'
#' This function applies the company-standard discount rate to
#' projected annual revenue.
#'
#' @param annual_rev Numeric vector of annual revenue.
#' @param churn_rate Numeric value between 0 and 1.
#' @return The calculated CLV.
#' @export
calculate_clv <- function(annual_rev, churn_rate) {
if (churn_rate <= 0) stop("Churn rate must be positive.")
return(annual_rev / churn_rate)
}
The @export tag is the magic part; it tells R that this function should be available to the user when they load the package, rather than being a hidden helper function used internally by other functions.
Solving the distribution nightmare
The biggest question I get is: "Where do I actually put the package so others can install it?" You don't need to submit it to CRAN. Most companies use an internal Git repository (GitLab, GitHub Enterprise, or Bitbucket).
Once your package is pushed to an internal repo, your colleagues can install it with a single line of code using the remotes package. This ensures everyone is using the exact same version of the logic. If you push a fix to the main branch, everyone just runs the install command again and they're up to date. No more emailing utils_v2_final_FIXED.R.
# The "Professional Way"
# install.packages("remotes")
remotes::install_git("https://git.company.com/analytics/corpdata.git")
library(corpdata)
calculate_clv(1000, 0.2)
📋 Practical Task
Build the 'corpfinance' Internal Utility Package
You have been tasked with creating the first official internal package for the Finance department. They are tired of copying and pasting a specific EBITDA (Earnings Before Interest, Taxes, Depreciation, and Amortization) calculation across ten different spreadsheets and scripts.
Your requirements:
- Create a package structure using
devtools::create("corpfinance"). - Create a function called
calc_ebitdathat takesrevenue,expenses,interest,taxes,depreciation, andamortizationas arguments. - The function should return the sum of (Revenue - Expenses) + Interest + Taxes + Depreciation + Amortization.
- Use
roxygen2tags to:- Provide a clear title and description of the function.
- Document all six parameters.
- Export the function so it is available to the user.
- Add
dplyras a dependency in theDESCRIPTIONfile (even if the function doesn't strictly need it yet, the finance team wants it available for the data frames they'll be passing in). - Run
devtools::document()to generate the help files.
Verification: To complete the task, ensure that when you run devtools::load_all(), you can call ?calc_ebitda and see the help page you authored.
There are no comments for now.