knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

You can use check_for(), check_while() and check_if_else() chains to check whether a student has properly coded control constructs.

Example 1: For loop

Consider the following solution:

# Print out the integers 1 to 10
for (i in 1:10) {
  print(i)
}

A suitable SCT for this exercise can be the following:

ex() %>% check_for() %>% {
  check_cond(.) %>% check_code("in\\s*1:10", missing_msg = "You can use `i in 1:10` to define your for loop.")
  check_body(.) %>% check_function("print") %>% check_arg("x") %>% check_equal(eval = FALSE)
}

Example 2: Check if else

Consider the following solution:

# Predefined value of x
x <- TRUE

# Code the if else construct
if (x) {
  print("x is TRUE!")
} else {
  print("x is not TRUE!")
}

The following SCT checks its correctness:

ex() %>% check_object("x")
ex() %>% check_if_else() %>% {
  check_cond(.) %>% check_code("x")
  check_if(.) %>% check_function("print") %>% check_arg("x") %>% check_equal()
  check_else(.) %>% check_function("print") %>% check_arg("x") %>% check_equal()
}

Example 3: Check if, else if, else

It is also possible to use else if in R. How to test this? Well, behind the scenes, R parses the following structure:

if (condition1) {
  expression
} else if (condition2) {
  expression
} else {
  expression
}

as if it follows this structure:

if (condition1) {
  expression
} else {
  if (condition2) {

  } else {

  }
}

If you want to test such a piece of code, you therefore need the following construct:

ex() %>% check_if_else() %>% {
  check_cond(.) %>% ...
  check_if(.) %>% ...
  check_else(.) %>% check_if_else() %>% {
    check_cond(.) %>% ...
    check_if(.) %>% ...
    check_else(.) %>% ...
  }
}

Caution

Not all of the SCT chains that you specify after a 'zooming in' step might work as you'd expect. Remember that functions like check_object() and check_function() often depend on both the student and solution environment. It is possible that during execution of control flow these values change. The testwhat functions will always compare the 'end environment' of student and solution, it is not possible to do matching on intermediate values of objects that are changed further in the script.



datacamp/testwhat documentation built on June 26, 2022, 9:07 a.m.