From 068d17d63a2bd79d4fda521fd1409e6c195bdff0 Mon Sep 17 00:00:00 2001 From: David Dorchies <david.dorchies@inrae.fr> Date: Tue, 5 Sep 2023 08:48:43 +0200 Subject: [PATCH] feat: add_report function Refs #3 --- .gitignore | 1 + NAMESPACE | 1 + R/add_report.R | 33 ++++++++++++++++++++++++++++ R/create_reports.R | 5 +++++ man/add_report.Rd | 29 ++++++++++++++++++++++++ man/create_reports.Rd | 5 +++++ tests/testthat/test-add_report.R | 18 +++++++++++++++ tests/testthat/test-create_fairify.R | 4 ++-- 8 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 R/add_report.R create mode 100644 man/add_report.Rd create mode 100644 tests/testthat/test-add_report.R diff --git a/.gitignore b/.gitignore index 118ad33..39cf9fd 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,4 @@ rsconnect/ /public /reports /templates +/.vscode diff --git a/NAMESPACE b/NAMESPACE index 16a90e1..c5aa1de 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export("%>%") export(add_gitignore) +export(add_report) export(create_fairify) export(create_reports) export(getDataPath) diff --git a/R/add_report.R b/R/add_report.R new file mode 100644 index 0000000..1e42367 --- /dev/null +++ b/R/add_report.R @@ -0,0 +1,33 @@ +#' Add a new report in a fairify project +#' +#' @param name Name of the report (sub-folder name in "reports") +#' @param path Path of the fairify project +#' +#' @return The path of the added report +#' @export +#' +#' @inherit create_reports examples +#' +add_report <- function(name, path = ".") { + if (!dir.exists(file.path(path, "reports"))) { + stop("Report structure not found, call `create_reports` first!") + } + destpath <- file.path(path, "reports", name) + if (dir.exists(destpath)) + stop("The folder ", + destpath, + " exists already. Delete it first or choose another.") + b <- file.copy( + from = file.path(pkg_sys("bookdown_template")), + to = file.path(path, "reports"), + recursive = TRUE, + copy.date = TRUE + ) + if (any(!b)) + stop("The copy of the template report failed") + file.rename( + file.path(path, "reports", "bookdown_template"), + destpath + ) + return(destpath) +} diff --git a/R/create_reports.R b/R/create_reports.R index 0a26b98..be11bed 100644 --- a/R/create_reports.R +++ b/R/create_reports.R @@ -7,9 +7,14 @@ #' @export #' #' @examples +#' # Create structure for reports #' path <- tempdir() #' create_reports(path) #' list.files(path, recursive = TRUE) +#' +#' # Add a new report +#' report_path <- add_report("my_report", path) +#' list.files(report_path) create_reports <- function(path = ".", overwrite = FALSE) { # Copy templates if (dir.exists(file.path(path, "templates"))) { diff --git a/man/add_report.Rd b/man/add_report.Rd new file mode 100644 index 0000000..dd13a4d --- /dev/null +++ b/man/add_report.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/add_report.R +\name{add_report} +\alias{add_report} +\title{Add a new report in a fairify project} +\usage{ +add_report(name, path = ".") +} +\arguments{ +\item{name}{Name of the report (sub-folder name in "reports")} + +\item{path}{Path of the fairify project} +} +\value{ +The path of the added report +} +\description{ +Add a new report in a fairify project +} +\examples{ +# Create structure for reports +path <- tempdir() +create_reports(path) +list.files(path, recursive = TRUE) + +# Add a new report +report_path <- add_report("my_report", path) +list.files(report_path) +} diff --git a/man/create_reports.Rd b/man/create_reports.Rd index dff8630..1684265 100644 --- a/man/create_reports.Rd +++ b/man/create_reports.Rd @@ -18,7 +18,12 @@ Use for side effect. Set reports folder structure } \examples{ +# Create structure for reports path <- tempdir() create_reports(path) list.files(path, recursive = TRUE) + +# Add a new report +report_path <- add_report("my_report", path) +list.files(report_path) } diff --git a/tests/testthat/test-add_report.R b/tests/testthat/test-add_report.R new file mode 100644 index 0000000..dc6a42d --- /dev/null +++ b/tests/testthat/test-add_report.R @@ -0,0 +1,18 @@ +test_that("add_report should raise error if reports does not exists", { + path <- tempfile(pattern = "dir") + dir.create(path, recursive = TRUE) + expect_error(add_report("test", path)) +}) + +test_that("add_report should create a report", { + path <- tempfile(pattern = "dir") + dir.create(path, recursive = TRUE) + create_reports(path) + add_report("test", path) + expect_true( + all(file.exists( + file.path(path, + "reports/test", + list.files(pkg_sys("bookdown_template"))) + ))) +}) diff --git a/tests/testthat/test-create_fairify.R b/tests/testthat/test-create_fairify.R index 330a8be..b7c981a 100644 --- a/tests/testthat/test-create_fairify.R +++ b/tests/testthat/test-create_fairify.R @@ -1,5 +1,5 @@ test_that("create_fairify works", { - path <- tempfile() + path <- tempfile(pattern = "dir") create_fairify(path, open = FALSE) project <- basename(path) expect_true(file.exists(file.path(path, paste0(project, ".Rproj")))) @@ -7,7 +7,7 @@ test_that("create_fairify works", { }) test_that("create_fairify with git = FALSE doesn't create git repoworks", { - path <- tempfile() + path <- tempfile(pattern = "dir") create_fairify(path, open = FALSE, git = FALSE) project <- basename(path) expect_false(file.exists(file.path(path, ".git"))) -- GitLab