01 / 21

Communicating
Limitations in R

Learn to identify and clearly express the tradeoffs in your R code: when to prioritize readability over performance, and how to document every decision so others can follow your reasoning.

R Programming

What Are Limitations?

Every piece of code you write comes with limitations: constraints on what it can do, how fast it runs, and how easy it is for another person to understand. A limitation is not a flaw: it is a deliberate boundary you chose (or accepted) when writing your solution.

Why Communicate Them?

Trust
Readers trust code more when the author openly states what it cannot do.
Collaboration
Teammates can improve your code faster when they know which tradeoffs you already considered.
Maintenance
Future you (or someone else) will need context to decide if a limitation still applies.

The Core Tradeoff: Readability vs Performance

Quality Optimization Effort Readability Performance Sweet Spot

As you optimize R code for performance, readability often drops. The goal is to find a sweet spot where both are acceptable, and to document why you chose that point.

What Makes Code Readable?

1
Clear variable names. Use \(\text{avg_temperature}\) instead of \(x\).
2
Step-by-step logic. Break complex operations into multiple lines rather than nesting everything.
3
Comments that explain why. Not what the code does, but why you wrote it that way.
4
Consistent style. Follow the tidyverse style guide or your team's conventions.

What Makes Code Performant?

1
Vectorized operations. Use built-in R functions that operate on entire vectors at once instead of looping.
2
Minimal memory copying. Avoid growing objects inside loops: pre-allocate vectors with \(\text{numeric}(n)\).
3
Efficient data structures. Use \(\text{data.table}\) or \(\text{matrix}\) over \(\text{data.frame}\) when speed matters.
4
Compiled code. Call C++ via \(\text{Rcpp}\) for the most critical bottlenecks.

Readable vs Performant: Side by Side

Readable Version
# Calculate mean of positive values positive_vals <- Filter( function(x) x > 0, data ) result <- mean(positive_vals)
Performant Version
# Vectorized: faster for large data result <- mean(data[data > 0])

The Tradeoff Spectrum

Maximally Readable Balanced Maximally Performant for-loop with comments apply() family + documentation Rcpp / C++ extensions

Most school and data-analysis R projects should sit on the left to center of this spectrum. Only move right when profiling reveals a real bottleneck.

How to Communicate Limitations

1
Identify the limitation. Ask: what can this code not handle? What assumptions does it make?
2
State the tradeoff. Explain what you gained and what you gave up (e.g., "chose readability over speed").
3
Document it in the right place. Use inline comments for small tradeoffs, README sections for major ones.
4
Suggest the alternative. Briefly note what a higher-performance (or more readable) approach would look like.
5
Quantify when possible. Include timing or memory estimates: "Runs in ~2s for 100k rows."

Example 1: Simple Comment

# LIMITATION: This for-loop is O(n) and not vectorized. # Chose readability for students learning loops. # For datasets > 100k rows, use: sum(data > threshold) count <- 0 for (val in data) { if (val > threshold) { count <- count + 1 } }

Example 2: Function Documentation

#' Calculate moving average #' #' @param x Numeric vector #' @param window Integer window size #' @return Numeric vector of same length #' #' @section Limitations: #' - Uses a simple for-loop; O(n * window). #' - For large datasets, consider zoo::rollmean() #' which is implemented in C. #' - Returns NA for the first (window - 1) values. moving_avg <- function(x, window) { n <- length(x) result <- rep(NA, n) for (i in window:n) { result[i] <- mean(x[(i - window + 1):i]) } result }

Example 3: README Documentation

## Known Limitations | Limitation | Reason | Alternative | |-----------------------|---------------------|-----------------------| | No NA handling | Keeps code simple | Use na.rm = TRUE | | Single-threaded | Portability | Use parallel::mclapply| | Max 1M rows tested | Student hardware | Test on server |

Types of Limitations

Performance
Code runs slowly on large inputs. State the size threshold and the faster alternative.
Scope
Code only handles a specific case (e.g., numeric data, no missing values). State assumptions.
Accuracy
Approximation or rounding introduces error. State the error bound or precision.
Dependency
Code relies on a specific package version or system. State the requirement clearly.

When Should You Optimize?

Is it too slow?
Profile with \(\text{system.time()}\)
Bottleneck found? No Keep readable Yes Optimize it Document the tradeoff! Document it too!

Common Mistakes

✕ No Comment at All
Writing complex vectorized code without explaining why you avoided a simpler loop. Others cannot judge the tradeoff.
✕ Premature Optimization
Sacrificing readability for speed before proving the code is actually slow. Always profile first.
✕ Vague Limitations
Writing "this could be faster" without stating the threshold, the alternative, or the cost of switching.

Summary Comparison

Readable Approach Performant Approach
Style Named variables, step-by-step Vectorized, compact one-liners
Speed Slower on large data Fast at scale
Learning Curve Easy for beginners Requires R expertise
When to Use Teaching, exploration, small data Production, large datasets
Documentation Note performance ceiling Note readability cost

Question 1 of 5

What is the primary purpose of documenting a limitation in your R code?

Question 2 of 5

A classmate writes a for-loop that works correctly on 500 rows. They want to optimize it immediately with Rcpp. What should they do first?

Question 3 of 5

Which of the following is the best limitation comment?

Question 4 of 5

Compared to Code A, Code B is:

Question 5 of 5

Where is the best place to document a major limitation that affects an entire R project?