Skip to content

gradethis does not work when namespaced  #364

Open

Description

@JosiahParry

gradethis must be loaded globally using library(gradethis) otherwise code_feedback cannot be found.

mocked <- gradethis::mock_this_exercise("rnorm(10)", "rnorm(10)")
gradethis::grade_this_code()(mocked)
#> #> (gradethis::grade_this_code())(mocked)
#> Error in do_grade_this(): could not find function "code_feedback"
#> <gradethis_graded_this_code: [Neutral]
#>   A problem occurred with the grading code for this exercise.
#> >

Created on 2024-07-02 with reprex v2.1.0

Activity

  1. gadenbuie commented on Jul 2, 2024

    @gadenbuie
    Member

    If you just want to compare code, you can call code_feedback() directly. It returns NULL if the user code is correct, or a (hopefully) helpful message as a character string if not.

    gradethis::code_feedback("rnorm(10)", "rnorm(100)")
    #> In `rnorm(10)`, I expected `100` where you wrote `10`.
  2. JosiahParry commented on Jul 3, 2024

    @JosiahParry
    Author

    Thanks! It's not a complete replacement. Instead, I can use gradethis::random_praise() when the result is correct (NULL)

  3. gadenbuie commented on Jul 3, 2024

    @gadenbuie
    Member
  4. JosiahParry commented on Jul 3, 2024

    @JosiahParry
    Author

    Thank you! I'm having a bit of a tough time following the documentation since I am trying to use gradethis outside of the context of learnr so using pass() and fail() doesn't make much sense to me because you need to pass in the .result which is available from grade_this_code() and not code_feedback().

  5. gadenbuie commented on Jul 3, 2024

    @gadenbuie
    Member

    using pass() and fail() doesn't make much sense to me because you need to pass in the .result

    pass() and fail() don't need a result, they just take a string with the message you want to show. Here's a simple example with a light-weight grade_this_code() that doesn't do any user code evaluation.

    solution <- "rnorm(10)"
    user_correct <- solution
    user_wrong <- "rnorm(100)"
    
    code_grade <- function(user_code, solution) {
      feedback <- gradethis::code_feedback(user_code, solution)
      
      if (is.null(feedback)) {
        gradethis::pass()
      } else {
        gradethis::fail(feedback, encourage = TRUE)
      }
    }
    
    code_grade(user_correct, solution)
    #> <gradethis_graded: [Correct] Amazing! Correct!>
    code_grade(user_wrong, solution)
    #> <gradethis_graded: [Incorrect]
    #>   In `rnorm(100)`, I expected `10` where you wrote `100`. That's okay:
    #>   you learn more from mistakes than successes. Let's do it one more
    #>   time.
    #> >

    You can get the final pass/fail message directly from $message, but the graded object carries a small amount of additional information you might find useful.

    str(code_grade(user_correct, solution))
    #> List of 4
    #>  $ message : 'glue' chr "Superb work! Correct!"
    #>  $ correct : logi TRUE
    #>  $ type    : NULL
    #>  $ location: NULL
    #>  - attr(*, "class")= chr [1:2] "gradethis_graded" "condition"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions