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
191: Currying and Partial Application in R
When people first encounter currying or partial application in R, they often make a very specific mistake: they assume that R functions are "curried by default," similar to how they work in languages like Haskell or F#. You'll see students try to "freeze" an argument by calling the function with only some of its parameters, expecting R to hand them back a new, specialized function.
"Wait, why isn't this returning a function?"
Let's look at a concrete example. Imagine you're building a tool to calculate sales tax for different regions. You've written a straightforward function that takes a tax rate and a price:
calculate_tax <- function(rate, price) {
return(price * rate)
}
Now, you realize you're doing a lot of calculations for New York (8.875%). To save yourself some typing, you might try this:
# This is the common mistake
ny_tax <- calculate_tax(0.08875)
# You expect ny_tax to be a function you can call later with a price...
# But R throws an error: "argument 'price' is missing, with no default"
Here is the reality: in R, if you call a function, it executes immediately. It doesn't "wait" for the remaining arguments by returning a function; it just fails because the required price argument is missing. R functions are not curried by default.
Using Closures for Partial Application
If you want to create a specialized version of a function—which is what partial application actually is—you have to explicitly wrap that function inside another function. This creates a "closure," where the inner function remembers the environment (and the variables) of the outer function.
Here is how I actually handle this in my own production code when I want to avoid external dependencies:
make_tax_calculator <- function(rate) {
function(price) {
calculate_tax(rate, price)
}
}
# Now we 'partially apply' the rate
ny_tax <- make_tax_calculator(0.08875)
ca_tax <- make_tax_calculator(0.0725)
# Now these behave like specialized functions
ny_tax(100) # Returns 8.875
ca_tax(100) # Returns 7.25
I find this approach incredibly useful when you're working with lapply or purrr::map. Instead of writing a messy anonymous function inside your loop, you can create a clean, named partial function beforehand.
True Currying: The Chain of Singles
Currying is slightly different from partial application. While partial application is about fixing *some* arguments, currying is the process of transforming a function that takes N arguments into a chain of N functions that each take exactly one argument.
It looks a bit academic, but it's a powerful pattern for building flexible pipelines. In R, it looks like this:
curried_add <- function(a) {
function(b) {
function(c) {
a + b + c
}
}
}
# To get the result, you chain the calls
result <- curried_add(10)(20)(30) # Returns 60
You won't do this every day in R, but understanding it allows you to use functional programming libraries more effectively. For instance, if you use the purrr package, the partial() function does exactly what our make_tax_calculator did, but in a more generic way that works for any function.
📋 Practical Task
Building a Specialized Temperature Converter
You are working on a climate data project. You have a general function that converts temperatures based on a provided offset and a multiplier:
convert_temp <- function(multiplier, offset, temp) {
return((temp * multiplier) + offset)
}
Your task is to use the concept of partial application to create two specialized functions using the convert_temp logic:
celsius_to_fahrenheit: This should fix themultiplierat 1.8 and theoffsetat 32.celsius_to_kelvin: This should fix themultiplierat 1 and theoffsetat 273.15.
Once you have created these two functions, test them by passing the value 25 (Celsius) into both. Your celsius_to_fahrenheit function should return 77, and your celsius_to_kelvin function should return 298.15.
There are no comments for now.