and they need to determine the mean of the age column. Ideally we'd want students to do
my.mean <- mean(df$age)
And we would use testthat to check that mean(df$age) is the correct answer:
test_that("mean age is correct", {expect_equal(my.mean, 65)})
But if a student is not sure of how to answer the question, they could calculate the mean themselves (by eye or with a calculator, not using code) and submit
my.mean <- 65
Is there a way to check that students are using code or specific functions to determine their answers?
test_that("mean age is correct", { << something that checks if the function mean was used to generate the answer >> })
I was unlear, not you. My suggestion is that answers be provided asreprex, which would allow the return value 65 to be evaluated in terms of how it was created. (Still involves parsing.)
As a thought answer: A return value (whether from a function or a primitive, like <-) isn't generally feasible to reverse engineer to its call. Take for example, the mean mpg of mtcars:
Only if the student is somehow required to cut-and-paste an answer, rather than manually entering it, is it here possible to distinguish between manual and programmatic, and then only because of floating point.
It looks like you're trying to check their code, rather than (or in addition to) their results.
I don't think there's any easy automated way to do this. Even if you test that they're using the mean function, they may decide to write their own function, or call it something else. This is really in the realm of code review, for which most people will still resort to the good old-fashioned eyeball.
What you're looking for is at least partially covered by the gradethis package: https://pkgs.rstudio.com/gradethis/articles/gradethis.html. You can provide a desired solution (or a series of solutions) in the code and then use grade_this_code() function to do the checking. There are examples available with gradethis::gradethis_demo().
Thanks for introducing me to this! Unfortunately we aren't able to use learnr/shiny since we are using our institute's JupyterHub. But I will bookmark this for future applications.