Skip to content

Commit

Permalink
Reorganize and prune functions (rstudio#61)
Browse files Browse the repository at this point in the history
* comment out unused functions, move fxns to separate R files

* remove learnr dependency (broken)

* move and edit view/add/remove tutorial fxns

waiting for feature to be implement in rstudio before putting back in

also removes learnr depedency

* remove dependencies (clean up scripts and files)

removes call to pryr in grade_learnr and replace with rlang equlivilant

move unused functions to archive.
these are flagged for deletion.

move old tutorial files to archive,
will be added in when api is finalized,
and still deemed necessary.

* lintr!

* remove test and result function from archive.

result was removed in rstudio#21
  • Loading branch information
chendaniely authored Aug 9, 2019
1 parent a4cf671 commit 1fd6873
Show file tree
Hide file tree
Showing 39 changed files with 439 additions and 482 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
^\.lintr$
^\.vscode$
^scripts/
^archive/
7 changes: 0 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@ LazyData: true
RoxygenNote: 6.1.1
Imports:
purrr,
crayon,
clisymbols,
pryr,
rlang,
rstudioapi,
glue,
knitr,
callr,
learnr (>= 0.9.2.9001),
readr,
checkmate,
testthat
Remotes:
Expand Down
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(add_tutorial)
export(check_code)
export(check_result)
export(condition)
Expand All @@ -15,10 +14,6 @@ export(grading_demo)
export(pass_if)
export(random_encourage)
export(random_praise)
export(result)
export(test)
export(test_result)
export(test_solutions)
export(view_tutorial)
importFrom(rlang,"%||%")
importFrom(utils,browseURL)
5 changes: 2 additions & 3 deletions R/check_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@
#' @param glue_pipe A glue string that returns the final message displayed when a user uses a pipe,
#' \code{$>$}. Defaults to \code{getOption("gradethis_glue_pipe")}.
#'
#' @return a \code{grader_graded} structure from \code{\link{result}}.
#' @return a \code{\link{graded}} object.
#' An incorrect message will describe the first way that the answer differs,
#' the message will be the content of the \code{correct}
#' argument.
#' the message will be the content of the \code{glue_pipe} argument.
#'
#' @export
#' @examples
Expand Down
34 changes: 34 additions & 0 deletions R/condition.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' Condition object
#' Captures what the user passes into \code{\link{pass_if}} or \code{\link{fail_if}},
#' figures out what type of object was passed into \code{x},
#' and returns a \code{grader_condition} object that will be passed into \code{evaluate_condi}
#'
#' @param x expression to be evaluated
#' @param message character string for message returned
#' @param correct logical whether the condition is the correct answer
#'
#' @return a \code{grader_condition} object that contains
#' the expression \code{x},
#' the message \code{message},
#' whether or not the expression is the correct answer or not, \code{correct},
#' the type of expression (formula, function, or value), \code{type}
#' @export
condition <- function(x, message, correct) {
type <-
if (rlang::is_formula(x)) {
"formula"
} else if (rlang::is_function(x)) {
"function"
} else {
"value"
}

ret <- list(
x = x,
message = message,
correct = correct,
type = type
)
class(ret) <- "grader_condition"
ret
}
4 changes: 0 additions & 4 deletions R/detect_mistakes.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ isolate_mismatch <- function(user, solution, i) {
NULL
}

# named expects a vector of length one
named <- function(vec) {
!is.null(names(vec)) && names(vec) != ""
}

real_name <- function(name) {
!is.null(name) && name != ""
Expand Down
51 changes: 0 additions & 51 deletions R/pass_fail_condi.R → R/evaluate_condition.R
Original file line number Diff line number Diff line change
@@ -1,54 +1,3 @@
#' Pass if condition matches
#' @template pass_fail_x_condition
#' @param message chracter string for message returned
#' @export
pass_if <- function(x, message = NULL) {
condition(x, message, correct = TRUE)
}

#' Fail if condition matches
#' @template pass_fail_x_condition
#' @param message chracter string for message returned
#' @export
fail_if <- function(x, message = NULL) {
condition(x, message, correct = FALSE)
}

#' Condition object
#' Captures what the user passes into \code{\link{pass_if}} or \code{\link{fail_if}},
#' figures out what type of object was passed into \code{x},
#' and returns a \code{grader_condition} object that will be passed into \code{evaluate_condi}
#'
#' @param x expression to be evaluated
#' @param message character string for message returned
#' @param correct logical whether the condition is the correct answer
#'
#' @return a \code{grader_condition} object that contains
#' the expression \code{x},
#' the message \code{message},
#' whether or not the expression is the correct answer or not, \code{correct},
#' the type of expression (formula, function, or value), \code{type}
#' @export
condition <- function(x, message, correct) {
type <-
if (rlang::is_formula(x)) {
"formula"
} else if (rlang::is_function(x)) {
"function"
} else {
"value"
}

ret <- list(
x = x,
message = message,
correct = correct,
type = type
)
class(ret) <- "grader_condition"
ret
}

#' Evaluates a condition
#'
#' @param condi a \code{grader} \code{\link{condition}} object
Expand Down
27 changes: 27 additions & 0 deletions R/get_code.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' Get Code
#'
#' Helper methods around \code{rlang::\link[rlang]{eval_tidy}}
#' to extract user code and solution code.
#'
#' @seealso \code{\link{check_result}}, \code{\link{test_result}}, and \code{\link{check_code}}
#' @export
#' @rdname get_code
#' @param user,solution,expr An expression or quosure to evaluate.
#' @param name Name to print if a \code{NULL} expression is provided.
#' @inheritParams rlang::eval_tidy
get_user_code <- function(user = NULL, env = rlang::caller_env()) {
get_code(user, "user", env = env)
}
#' @export
#' @rdname get_code
get_solution_code <- function(solution = NULL, env = rlang::caller_env()) {
get_code(solution, "solution", env = env)
}
#' @export
#' @rdname get_code
get_code <- function(expr = NULL, name = "<name not provided>", env = rlang::caller_env()) {
if (is.null(expr)) {
stop("'", name, "' not provided")
}
rlang::eval_tidy(expr, env = env)
}
34 changes: 2 additions & 32 deletions R/grade_learnr.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' For exercise checking, learnr tutorials require a function that learnr can
#' use in the background to run the code in each "-check" chunk and to format
#' the results into a format that learnr can display. The function must accept a
#' specific set of inputs and return a specific type of output (see \code{\link{result}}).
#' specific set of inputs and return a specific type of output (see \code{\link{graded}}).
#' Users are not intended to use the function themselves, but to pass it to the
#' \code{exercise.checker} knitr chunk option within the setup chunk of the
#' tutorial.
Expand Down Expand Up @@ -91,7 +91,7 @@ grade_learnr <- function(label = NULL,
eval(parsed_check_code[[i]], envir_prep)
}
}
grading_code <- pryr::standardise_call(parsed_check_code[[length(parsed_check_code)]],
grading_code <- rlang::call_standardise(parsed_check_code[[length(parsed_check_code)]],
envir_prep)

# get all grader args
Expand Down Expand Up @@ -160,33 +160,3 @@ grade_learnr <- function(label = NULL,

ret
}



#' Get Code
#'
#' Helper methods around \code{rlang::\link[rlang]{eval_tidy}}
#' to extract user code and solution code.
#'
#' @seealso \code{\link{check_result}}, \code{\link{test_result}}, and \code{\link{check_code}}
#' @export
#' @rdname get_code
#' @param user,solution,expr An expression or quosure to evaluate.
#' @param name Name to print if a \code{NULL} expression is provided.
#' @inheritParams rlang::eval_tidy
get_user_code <- function(user = NULL, env = rlang::caller_env()) {
get_code(user, "user", env = env)
}
#' @export
#' @rdname get_code
get_solution_code <- function(solution = NULL, env = rlang::caller_env()) {
get_code(solution, "solution", env = env)
}
#' @export
#' @rdname get_code
get_code <- function(expr = NULL, name = "<name not provided>", env = rlang::caller_env()) {
if (is.null(expr)) {
stop("'", name, "' not provided")
}
rlang::eval_tidy(expr, env = env)
}
9 changes: 0 additions & 9 deletions R/order_calls.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ pre_pipe <- function(code, name = "") {
}
}

repipe <- function(lst, .call = FALSE) {
text <- purrr::map(lst, deparse)
text <- purrr::reduce(text, paste, sep = " %>% ")
text <- gsub("\\(NULL\\)", "()", text)

if (.call) parse(text = text)[[1]]
else text
}

renest <- function(lst, .call = FALSE) {
lst <- rev(lst)

Expand Down
15 changes: 15 additions & 0 deletions R/pass_fail_if.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#' Pass if condition matches
#' @template pass_fail_x_condition
#' @param message chracter string for message returned
#' @export
pass_if <- function(x, message = NULL) {
condition(x, message, correct = TRUE)
}

#' Fail if condition matches
#' @template pass_fail_x_condition
#' @param message chracter string for message returned
#' @export
fail_if <- function(x, message = NULL) {
condition(x, message, correct = FALSE)
}
25 changes: 0 additions & 25 deletions R/result.R

This file was deleted.

20 changes: 1 addition & 19 deletions R/test_result.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#' @template learnr_args
#' @param ... ignored
#'
#' @return a \code{grader_graded} structure from \code{\link{result}} containing
#' @return a \code{grader_graded} structure from \code{\link{graded}} containing
#' a formatted \code{correct} or \code{incorrect} message.
#' @seealso \code{test}
#' @export
Expand Down Expand Up @@ -125,21 +125,3 @@ grader_tests <- function(...) {
)
)
}
#' TODO document with 'tests' documentation
#' @export
#' @rdname test
#' @param message Message to report back if the test throws an error.
#' @param fn function to execute against the user solution.
#' If the test fails, it should throw an error to display the \code{message} provided.
test <- function(message, fn) {
function(x) {
tryCatch(
{ # nolint
fn(x)
},
error = function(e) {
stop(message, call. = FALSE)
}
)
}
}
Loading

0 comments on commit 1fd6873

Please sign in to comment.