Week 3 · Advanced Statistical Programming using R
Which function name best follows the naming guidance from the lecture?
my_function(data)f(x)summarise_by_species(df)test2(penguins)Answer: 3) A function’s name should describe what it does, not what it is.
What is the core idea of the Outside-In approach?
Answer: 3) Outside-In starts from the interface: output <- what_am_i_trying_to_do(information_piece, another_piece) first. Ask the three questions from lecture 2 about Task, Inputs & Output.
R distinguishes between three types of conditions. Which set is correct?
Answer: 4) An Error stops execution, a Warning lets the code finish but signals possible issues, and a Message is informational only.
You run the following code and get an error:
What is the most likely cause?
mtcars does not contain the column cylfilter() (here dplyr) has not been loaded|> is not supported in base RAnswer: 2) filter() is a dplyr function, but only tidyr was loaded. Add library(dplyr). “Could not find function” almost always means a missing library() call.
You want to ask for help to fix a bug. Which of the following is NOT a good principle for a Minimal Reproducible Example (MRE)?
mtcars or create small example datalibrary() calls needed to run the codeAnswer: 3) “Minimal” means the fewest lines that still reproduce the problem.
What does dput() do, and why is it useful when sharing a problem online?
Answer: 2) Use dput() to convert data to code if you must share real data.
What does traceback() (or rlang::last_trace()) show you after an error?
Answer: 2) Traceback shows the call sequence leading up to an error, which is useful for understanding where the error occurred in our nested code.
You inserted browser() in your function. R pauses and shows Browse[1]>. You type n and press Enter. What happens?
Answer: 4) n = next: run the next line, then pause again. Other key commands: s step into, f finish, c continue, Q quit.
Which statement about debugonce(fn) or debug(fn) is correct?
debugonce() runs faster than debug(fn)debug() only works on functions you wrote yourselfdebug() triggers on every call until undebug() is rundebugonce() can automatically fix simple bugsAnswer: 3) debug(fn) keeps pausing on every invocation until you call undebug(fn). debugonce(fn) triggers exactly once and auto-removes.
Your favourite LLM wrote you some R functions. Which actions follow the guidelines from the lecture? (multiple correct answers)
Answer: 1, 2 & 4) Exact checks (1) verify on known input, judgement checks (2) does the reasoning make sense?, stability checks (4) re-run for stochastic output. Answer 3 is wrong, because code that runs without errors can still produce silent failures.