Bird
Raised Fist0
R Programmingprogramming~20 mins

Parameterized reports in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Parameterized Reports Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of parameterized R Markdown report chunk
Consider this R Markdown chunk that uses a parameter n to generate a sequence. What will be the output when n = 4?
R Programming
params <- list(n = 4)
seq_output <- seq_len(params$n)
print(seq_output)
A[1] 1 2 3 4
B[1] 0 1 2 3 4
C[1] 1 2 3
DError: object 'params' not found
Attempts:
2 left
💡 Hint
Remember that seq_len(n) generates a sequence from 1 to n.
🧠 Conceptual
intermediate
1:30remaining
Purpose of parameters in R Markdown reports
What is the main purpose of using parameters in R Markdown reports?
ATo speed up report rendering by caching results
BTo convert R Markdown files into HTML only
CTo automatically generate plots without user input
DTo allow dynamic input values that change report output without editing code
Attempts:
2 left
💡 Hint
Think about how parameters help customize reports.
🔧 Debug
advanced
2:30remaining
Identify the error in parameter usage
This R Markdown snippet tries to use a parameter year to filter data but causes an error. What is the error?
R Programming
params <- list(year = 2023)
data <- data.frame(year = c(2022, 2023, 2024), value = c(10, 20, 30))
filtered <- subset(data, year == year)
print(filtered)
AThe parameter 'year' is not defined before use
BThe filter condition uses the same name 'year' for column and parameter causing ambiguity
CThe subset function is misspelled
DThe data frame 'data' is empty
Attempts:
2 left
💡 Hint
Check variable names in the filter condition carefully.
📝 Syntax
advanced
2:00remaining
Correct syntax to define parameters in YAML header
Which YAML header snippet correctly defines a parameter threshold with default value 10 for an R Markdown report?
A
params:
  - threshold: 10
B
parameters:
  threshold = 10
C
params:
  threshold: 10
D
parameters:
  threshold: "10"
Attempts:
2 left
💡 Hint
YAML uses key-value pairs with colon and indentation.
🚀 Application
expert
2:30remaining
How many rows will the filtered data have?
Given this R code in a parameterized report with parameter min_val = 15, how many rows will filtered_data contain?
R Programming
params <- list(min_val = 15)
data <- data.frame(id = 1:5, score = c(10, 15, 20, 25, 30))
filtered_data <- subset(data, score > params$min_val)
print(nrow(filtered_data))
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Count how many scores are strictly greater than 15.

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

  1. Step 1: Understand parameterized reports concept

    Parameterized reports allow passing different inputs to the same report code.
  2. Step 2: Identify the main benefit

    This lets you customize the report output without editing the code itself.
  3. Final Answer:

    To customize report content without changing the code -> Option B
  4. 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

  1. Step 1: Recall YAML parameter syntax in R Markdown

    Parameters are defined under params: with key-value pairs.
  2. Step 2: Check correct key and value format

    params: year: 2023 uses params: and year: 2023 correctly without quotes needed for numbers.
  3. Final Answer:

    params: year: 2023 -> Option D
  4. 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

  1. Step 1: Check the condition with given parameter

    The condition checks if params$region equals "West". It is set to "West".
  2. Step 2: Determine which print statement runs

    Since condition is true, it prints "Sales are high in the West".
  3. Final Answer:

    [1] "Sales are high in the West" -> Option A
  4. 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:
---
title: "Sales Report"
params:
  month = "January"
output: html_document
---
medium
A. Incorrect output format specified
B. Missing quotes around 'January'
C. Using '=' instead of ':' to assign parameter value
D. Missing 'params' section

Solution

  1. Step 1: Review YAML parameter syntax

    Parameters use colon ':' to assign values, not equals '='.
  2. Step 2: Identify the syntax error

    Here, month = "January" uses '=' which is invalid YAML syntax.
  3. Final Answer:

    Using '=' instead of ':' to assign parameter value -> Option C
  4. 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)
B. filtered_df <- df[df$category = params$category & df$year = params$year, ]
C. filtered_df <- filter(df, category = params$category, year = params$year)
D. filtered_df <- df[category == params$category & year == params$year]

Solution

  1. Step 1: Understand filtering syntax in R

    The subset() function uses logical conditions with '==' to filter rows.
  2. Step 2: Check parameter usage in filtering

    filtered_df <- subset(df, category == params$category & year == params$year) correctly uses params$category and params$year with '==' inside subset().
  3. Final Answer:

    filtered_df <- subset(df, category == params$category & year == params$year) -> Option A
  4. Quick Check:

    Use '==' for comparison and subset() for filtering [OK]
Hint: Use subset() with '==' and params$ for filtering [OK]
Common Mistakes:
  • Using '=' instead of '==' for comparison
  • Incorrect subsetting syntax without dataframe reference
  • Confusing filter() syntax without library
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard