Skip to content

Commit

Permalink
add example helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
maxheld83 committed Jul 7, 2024
1 parent a2c8090 commit bc24993
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ Language: en
Roxygen: list(markdown = TRUE)
Imports:
cli,
fs,
pkgload,
purrr,
rlang
rlang,
usethis
Suggests:
rstudioapi,
shinytest2,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
withr
Config/testthat/edition: 3
Config/testthat/parallel: true
Config/Needs/website: rmarkdown
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export(path_ex_file)
export(path_extdata)
export(path_package_this)
export(transform_with_generated)
export(use_ex_file)
28 changes: 28 additions & 0 deletions R/examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ source_pef <- function(...) {
source(path_ex_file(...), local = TRUE)$value
}

#' Create or edit example files
#'
#' This function creates R files in the `inst/examples/` to be reused in
#' tests, `#' @examples` and more.
#' When reused for tests, only last return of an example file will be tested.
#' You may therefore need a lot of relatively small example files.
#' The suggested convention is to sort them in folders named by `file_name`
#' function name and test name.
#'
#' @inheritParams fs::path
#' @inheritParams usethis::use_template
#' @export
#' @keywords example helpers
use_ex_file <- function(..., open = rlang::is_interactive()) {
# TODO https://github.com/dataheld/elf/issues/2
# take currently open file name from usethis
usethis::use_directory(example_path)
path <- fs::path(example_path, ..., ext = "R")
usethis::use_template(
"example.R",
save_as = path,
open = open,
package = "elf"
)
}

example_path <- fs::path("inst", "examples")

#' Create path to example files
#' Used in example tag and tests
#' @inheritParams path_package_this
Expand Down
1 change: 1 addition & 0 deletions inst/templates/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# only last return can be tested

0 comments on commit bc24993

Please sign in to comment.