BEFORE:
267 lines total
AFTER:
611 lines total
(+344 lines, +128% growth)
## Function Arguments
### Default Arguments
[basic example]
### Named Arguments
[basic example]
**Best practice**: Use named arguments...
Total: ~50 lines
Subsections: 2
Focus: Syntax
Now called: "Calling Functions: Type Expectations and Parameter Requirements"
## Function Arguments
### Default Arguments
[retained]
### Named Arguments
[retained]
## Calling Functions: Type Expectations and Parameter Requirements
### Understanding Type Requirements
- 7 common data types explained
- Why type matters
### Type Mismatch: Common Errors
- Example 1: Numeric Expected, String Supplied
* Shows error and why it fails
- Example 2: Vector Expected, Single Value Supplied
* Shows error and why it fails
- Example 3: Data Frame Expected, Vector Supplied
* Shows error and why it fails
### Why Type Matters: Silent Wrong Results
- R sometimes doesn't error!
- Shows dangerous scenario: medication dose calculation
- Demonstrates vectorized output surprise
- Explains why validation is critical
### Type Checking: Validation
- How to validate input types
- `is.numeric()`, `is.character()` examples
- Using `stop()` with clear error messages
- Real function with validation
### Calling Functions: Step-by-Step
- Step 1: Know What the Function Expects
* Reading function definitions
* Understanding argument types
- Step 2: Supply Arguments with Correct Types
* ✓ CORRECT examples
* ❌ WRONG examples
- Step 3: Verify Results Make Sense
* Sanity checking
* Typo detection
### Common Type Issues and Solutions
- Reference table: 5 common issues
- String vs. Numeric conversion
- Vector vs. Single value extraction
- Factor vs. Character conversion
- Missing value handling
### Parameter Expectations: What Happens When Not Met
- Case 1: Explicit Error (Best)
- Case 2: Silent Wrong Results (Dangerous)
- Case 3: Unexpected Behavior
### Best Practices for Calling Functions
1. Check function documentation
2. Verify argument types before calling
3. Use explicit conversion when needed
4. Test with known values first
5. Add defensive checks in your own functions
Total: ~350 lines
Subsections: 8 major + 10+ sub-subsections
Focus: Safe function calling, type safety, validation
| Concept | Lines | Examples |
|---|---|---|
| Type requirements explanation | 15 | Data type list |
| Example 1: String instead of numeric | 25 | calculate_bmi("70", 1.75) |
| Example 2: Vector instead of single | 20 | calculate_average("120") |
| Example 3: Vector instead of data frame | 25 | get_patient_age(ages) |
| Silent wrong results (dangerous!) | 40 | Vectorized output surprise |
| Type validation patterns | 30 | is.numeric(), stop() |
| Step-by-step calling guide | 50 | 3 steps + examples |
| Type conversion examples | 25 | as.numeric(), indexing |
| Reference table | 15 | 5 common issues |
| Three outcomes explanation | 50 | Explicit error, silent fail, unexpected |
| Best practices | 35 | 5 practices with code |
- No mention of clinical safety
- No emphasis on validation
- Type errors seemed optional
- Medication dosing example — Wrong type = wrong dose!
- Patient data processing — Data frames vs. vectors
- Validation as quality assurance — Prevents errors
- Silent failures are dangerous — Emphasized multiple times
- Clinical context throughout — Lab values, patient data, dosing
Example added in "Silent Wrong Results" section:
# CLINICAL DANGER: Function without validation
bad_summarize <- function(values) {
mean(values) # No type checking
}
bad_summarize(c("120", "125", "118")) # Returns NA silently
# In clinical context: wrong lab value average reported!- What is a function?
- How do I write one?
- How do I call one with default arguments?
- What is a function?
- How do I write one?
- How do I safely call one?
- What types do functions expect?
- What happens when types don't match?
- How do I validate inputs?
- How do I debug type errors?
- What are best practices?
| Category | Before | After |
|---|---|---|
| Basic examples | 3 | 3 (retained) |
| Type error examples | 0 | 3 |
| Validation examples | 0 | 2 |
| Wrong result examples | 0 | 3 |
| Conversion examples | 0 | 3 |
| Reference functions | 24 | 24 (retained) |
| Total | 27 | 44 |
- Syntax-focused
- Mechanical understanding
- "Here's what you can do"
- Safety-focused
- Error prevention
- "Here's what can go wrong and how to prevent it"
- Clinical emphasis on data integrity
- Someone learning "what is a function"
- General R learner
- Someone learning "how to call functions safely"
- Clinical researcher who needs reliable calculations
- Programmer building defensive functions
- Anyone who needs to understand type safety
"Functions make code reusable"
"Use default arguments to make functions flexible"
"Use named arguments for clarity"
"Functions EXPECT specific types"
"WRONG TYPES cause errors or SILENT WRONG RESULTS"
"Always VALIDATE inputs in clinical code"
"Silent failures are DANGEROUS in clinical context"
"TYPE SAFETY is not optional—it's a requirement"
"Defensive programming prevents errors before they happen"
- Good for learning function basics
- No guidance on validation
- No warning about type safety
- Could lead to undetected errors
- Ready for clinical use
- Clear validation patterns
- Strong emphasis on type safety
- Examples show dangerous scenarios
- Best practices prevent errors