📖 Scenario: You work in a small bookstore. You want to create a report that shows sales data for a specific month. Instead of making a new report every time, you will make a parameterized report that can show data for any month you choose.
🎯 Goal: Build a simple R script that uses a parameter (month) to filter sales data and then prints the total sales for that month.
📋 What You'll Learn
Create a data frame called sales_data with columns month and sales and exact values.
Create a variable called selected_month to hold the month to filter.
Use a filter to select rows from sales_data where month matches selected_month.
Calculate the total sales for the selected month and store it in total_sales.
Print the total sales with a clear message.
💡 Why This Matters
🌍 Real World
Stores and businesses often need reports filtered by dates or categories. Parameterized reports save time and avoid repeating work.
💼 Career
Data analysts and business intelligence professionals use parameterized reports to quickly generate insights for different time periods or segments.
Progress0 / 4 steps
1
Create the sales data frame
Create a data frame called sales_data with two columns: month and sales. Use these exact values: months are "January", "February", "March", "April", and sales are 150, 200, 170, 220 respectively.
R Programming
Hint
Use data.frame() with two vectors named month and sales.
2
Set the selected month parameter
Create a variable called selected_month and set it to the string "March".
R Programming
Hint
Assign the string "March" to selected_month.
3
Filter data and calculate total sales
Create a variable called filtered_data that contains only rows from sales_data where month equals selected_month. Then create a variable called total_sales that sums the sales column of filtered_data.
R Programming
Hint
Use sales_data[sales_data$month == selected_month, ] to filter rows and sum() to add sales.
4
Print the total sales
Write a print() statement that outputs the text: Total sales in March: 170 using the variable total_sales and selected_month.
R Programming
Hint
Use paste() inside print() to combine text and variables.
Practice
(1/5)
1. What is the main purpose of using parameterized reports in R Markdown?
easy
A. To speed up the knitting process
B. To customize report content without changing the code
C. To automatically generate plots
D. To create interactive dashboards
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 B
Quick Check:
Parameterized reports = customize content without code change [OK]
Hint: Think: change inputs, not code, to customize reports [OK]
Common Mistakes:
Confusing parameterized reports with speeding up knitting
Thinking it automatically creates plots
Mixing with dashboard creation
2. Which YAML header syntax correctly defines a parameter named year with default value 2023?
easy
A. params:
year: "2023"
B. parameters:
year = 2023
C. parameters:
year: 2023
D. params:
year: 2023
Solution
Step 1: Recall YAML parameter syntax in R Markdown
Parameters are defined under params: with key-value pairs.
Step 2: Check correct key and value format
params:
year: 2023 uses params: and year: 2023 correctly without quotes needed for numbers.
Final Answer:
params:
year: 2023 -> Option D
Quick Check:
YAML params use 'params:' and colon syntax [OK]
Hint: Use 'params:' and colon, no equals sign in YAML [OK]
Common Mistakes:
Using 'parameters:' instead of 'params:'
Using equals sign '=' instead of colon ':'
Putting numbers in quotes unnecessarily
3. Given this R Markdown snippet, what will be the output when params$region is set to "West"?
```{r}
if (params$region == "West") {
print("Sales are high in the West")
} else {
print("Sales data not available")
}
```
medium
A. [1] "Sales are high in the West"
B. [1] "Sales data not available"
C. Error: object 'params' not found
D. [1] "Sales are high in the East"
Solution
Step 1: Check the condition with given parameter
The condition checks if params$region equals "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 A
Quick Check:
params$region == "West" -> print message for West [OK]
Hint: Match param value exactly to choose output branch [OK]
Common Mistakes:
Confusing the else branch output
Assuming params is undefined causing error
Mixing region names in output
4. Identify the error in this R Markdown YAML header for parameterized report:
C. Using '=' instead of ':' to assign parameter value
D. Missing 'params' section
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 C
Quick Check:
YAML params need ':' not '=' [OK]
Hint: YAML uses colon ':' for key-value pairs, never '=' [OK]
Common Mistakes:
Using '=' instead of ':' in YAML
Thinking quotes are mandatory for strings
Confusing output format with params syntax
5. You want to create a parameterized report that filters a dataset by category and year. Which approach correctly uses parameters in the R code chunk to filter df?
hard
A. filtered_df <- subset(df, category == params$category & year == params$year)