Parameterized reports in R Programming - Time & Space Complexity
When creating parameterized reports in R, it's important to know how the report's run time changes as the input data grows.
We want to find out how the time to generate the report grows when we change the size of the data or parameters.
Analyze the time complexity of the following code snippet.
library(dplyr)
generate_report <- function(data, filter_value) {
filtered_data <- data %>% filter(category == filter_value)
summary <- filtered_data %>% summarise(count = n(), avg = mean(value))
return(summary)
}
# Example call:
# generate_report(large_data_frame, "A")
This code filters a data frame by a parameter and then summarizes the filtered results.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Filtering the data frame rows based on the parameter.
- How many times: Each row is checked once during filtering.
As the data size grows, the filtering step checks more rows, so the time grows roughly in proportion to the number of rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks |
| 100 | About 100 row checks |
| 1000 | About 1000 row checks |
Pattern observation: The work grows directly with the number of rows in the data.
Time Complexity: O(n)
This means the time to generate the report grows linearly as the data size increases.
[X] Wrong: "Filtering by a parameter is instant and does not depend on data size."
[OK] Correct: Filtering must check each row to see if it matches, so it takes longer with more rows.
Understanding how filtering and summarizing scale with data size helps you explain report performance clearly and confidently.
"What if we indexed the data by category before filtering? How would the time complexity change?"
Practice
parameterized reports in R Markdown?Solution
Step 1: Understand parameterized reports concept
Parameterized reports allow passing different inputs to the same report code.Step 2: Identify the main benefit
This lets you customize the report output without editing the code itself.Final Answer:
To customize report content without changing the code -> Option BQuick Check:
Parameterized reports = customize content without code change [OK]
- Confusing parameterized reports with speeding up knitting
- Thinking it automatically creates plots
- Mixing with dashboard creation
year with default value 2023?Solution
Step 1: Recall YAML parameter syntax in R Markdown
Parameters are defined underparams:with key-value pairs.Step 2: Check correct key and value format
params: year: 2023 usesparams:andyear: 2023correctly without quotes needed for numbers.Final Answer:
params: year: 2023 -> Option DQuick Check:
YAML params use 'params:' and colon syntax [OK]
- Using 'parameters:' instead of 'params:'
- Using equals sign '=' instead of colon ':'
- Putting numbers in quotes unnecessarily
params$region is set to "West"?
```{r}
if (params$region == "West") {
print("Sales are high in the West")
} else {
print("Sales data not available")
}
```Solution
Step 1: Check the condition with given parameter
The condition checks ifparams$regionequals "West". It is set to "West".Step 2: Determine which print statement runs
Since condition is true, it prints "Sales are high in the West".Final Answer:
[1] "Sales are high in the West" -> Option AQuick Check:
params$region == "West" -> print message for West [OK]
- Confusing the else branch output
- Assuming params is undefined causing error
- Mixing region names in output
--- title: "Sales Report" params: month = "January" output: html_document ---
Solution
Step 1: Review YAML parameter syntax
Parameters use colon ':' to assign values, not equals '='.Step 2: Identify the syntax error
Here,month = "January"uses '=' which is invalid YAML syntax.Final Answer:
Using '=' instead of ':' to assign parameter value -> Option CQuick Check:
YAML params need ':' not '=' [OK]
- Using '=' instead of ':' in YAML
- Thinking quotes are mandatory for strings
- Confusing output format with params syntax
category and year. Which approach correctly uses parameters in the R code chunk to filter df?Solution
Step 1: Understand filtering syntax in R
Thesubset()function uses logical conditions with '==' to filter rows.Step 2: Check parameter usage in filtering
filtered_df <- subset(df, category == params$category & year == params$year) correctly usesparams$categoryandparams$yearwith '==' insidesubset().Final Answer:
filtered_df <- subset(df, category == params$category & year == params$year) -> Option AQuick Check:
Use '==' for comparison and subset() for filtering [OK]
- Using '=' instead of '==' for comparison
- Incorrect subsetting syntax without dataframe reference
- Confusing filter() syntax without library
Ready to prove it?
Pass the test and this pattern moves unknown
