From 550d9212fd3beb8751f1d6acb0407e581ecdc586 Mon Sep 17 00:00:00 2001 From: Daniel Schreyer Date: Mon, 11 Nov 2024 14:18:27 +0100 Subject: [PATCH] introducing rstudio addin for dso repro stage (#19) * introducing rstudio addin for dso repro stage * changed docu based on warnings * updated stage addin; removed reload ... param * included dso repro stage with dependencies * pre-commit autofixes * styler * updated addin functions for better usage * pre-commit autofixes * updated addin; included repro function * pre-commit autofixes * fixed dvc.yaml error, removed show report * removed test_stage * pre-commit autofixes * changelog fix and dvc_yaml_path removal --------- Co-authored-by: DSchreyer --- CHANGELOG.md | 12 ++++ DESCRIPTION | 5 +- NAMESPACE | 4 ++ R/addin.R | 73 +++++++++++++++++++++ R/repro.R | 32 +++++++++ inst/rstudio/addins.dcf | 9 +++ man/dso_repro_stage_addin.Rd | 20 ++++++ man/dso_repro_stage_w_dependencies_addin.Rd | 20 ++++++ man/repro.Rd | 24 +++++++ 9 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 R/addin.R create mode 100644 R/repro.R create mode 100644 inst/rstudio/addins.dcf create mode 100644 man/dso_repro_stage_addin.Rd create mode 100644 man/dso_repro_stage_w_dependencies_addin.Rd create mode 100644 man/repro.Rd diff --git a/CHANGELOG.md b/CHANGELOG.md index fc59da8..990f503 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v0.6 + +### New Features + +- Introduced the `repro` function that performs `dso repro` of a single stage with or without its dependencies +- Introduced the corresponding rstudio addins for the `repro` function + +### Fixes + +- Removed `...` in reload function + + ## v0.5.1 ### Improvements diff --git a/DESCRIPTION b/DESCRIPTION index 320108a..1cf4126 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: dso Type: Package Title: dso R companion package -Version: 0.5.1 +Version: 0.6 Author: Daniel Schreyer, Gregor Sturm, Thomas Schwarzl @@ -18,7 +18,8 @@ Imports: here, stringr, methods, - rlang + rlang, + rstudioapi (>= 0.11) RoxygenNote: 7.3.2 Suggests: testthat (>= 3.0.0) diff --git a/NAMESPACE b/NAMESPACE index a006423..3b06293 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,9 +6,12 @@ S3method(print,dsoParams) export(compile_config) export(create_stage) export(dsoParams) +export(dso_repro_stage_addin) +export(dso_repro_stage_w_dependencies_addin) export(init) export(read_params) export(reload) +export(repro) export(safe_get) export(set_stage) export(stage_here) @@ -20,6 +23,7 @@ importFrom(here,here) importFrom(here,i_am) importFrom(methods,show) importFrom(rlang,caller_env) +importFrom(rstudioapi,viewer) importFrom(stringr,coll) importFrom(stringr,str_match_all) importFrom(stringr,str_replace) diff --git a/R/addin.R b/R/addin.R new file mode 100644 index 0000000..97afd4c --- /dev/null +++ b/R/addin.R @@ -0,0 +1,73 @@ +#' Execute dso repro and display the result +#' +#' This function reproduces the current stage without its dependencies +#' and displays the resulting report in the RStudio Viewer pane. +#' +#' @return None +#' @export +#' @importFrom glue glue +#' @importFrom rstudioapi viewer +#' @examples +#' \dontrun{ +#' dso_repro_stage_addin() +#' } +dso_repro_stage_addin <- function() { + check_stage_here() + + repro(stage_here(), single_stage = TRUE) + + check_report() +} + +#' Execute dso repro with dependencies and display the result +#' +#' This function reproduces the current stage along its dependencies +#' and displays the resulting report in the RStudio Viewer pane. +#' +#' @return None +#' @export +#' @importFrom glue glue +#' @importFrom rstudioapi viewer +#' @examples +#' \dontrun{ +#' dso_repro_stage_w_dependencies_addin() +#' } +dso_repro_stage_w_dependencies_addin <- function() { + check_stage_here() + + repro(stage_here(), single_stage = FALSE) + + check_report() +} + +#' Get the current stage path +#' +#' This function retrieves the absolute path to the current stage. +#' If the stage is not defined, it stops with an error message. +#' +#' @return The absolute path to the current stage. +#' @noRd +check_stage_here <- function() { + if (length(stage_here()) == 0) { + stop(glue::glue("stage_here() is not defined. Please read in your config file using read_params().")) + } +} + +#' Check and display the report +#' +#' This function checks for the generated report files in the stage directory +#' +#' @return None +#' @noRd +#' @importFrom glue glue +#' @importFrom rstudioapi viewer +check_report <- function() { + report_files <- list.files(stage_here("report"), pattern = "\\.pdf$|\\.html$", full.names = TRUE) + + if (length(report_files) == 1) { + report_file <- report_files[1] + message(glue::glue("Report generated: {report_file}")) + } else { + stop(glue::glue("No report file or multiple report files were identified. Please check the report directory.")) + } +} diff --git a/R/repro.R b/R/repro.R new file mode 100644 index 0000000..f81a185 --- /dev/null +++ b/R/repro.R @@ -0,0 +1,32 @@ +#' @title Reproduce a dso stage +#' @description +#' Reproduce a dso stage with or without its dependencies. +#' @details +#' This function reproduces a stage specified by `stage_path`. +#' If `single_stage` is set to `TRUE`, +#' it reproduces the stage without its dependencies. +#' Otherwise, it reproduces the current stage +#' along with all its dependency stages. +#' By default, the current stage will be reproduced. +#' @param stage_path The path to a stage. Defaults to the current stage by using `stage_here()`. +#' @param single_stage Logical flag indicating whether to reproduce only the current stage (`TRUE`) or the current stage with all dependencies (`FALSE`). Defaults to `FALSE`. +#' @export +repro <- function(stage_path = stage_here(), single_stage = F) { + if (single_stage) { + message(glue::glue("Reproducing the current stage with all its dependency stages.")) + repro_cmd <- "repro" + } else { + message(glue::glue("Reproducing the current stage without dependencies.")) + repro_cmd <- "repro -s" + } + result <- system2( + DSO_EXEC, + c(repro_cmd, shQuote(file.path(stage_path, "dvc.yaml"))) + ) + + if (result != 0) { + stop(glue::glue("DSO {repro_cmd} failed with status: {result}")) + } else { + message("dso repro was executed successfully") + } +} diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf new file mode 100644 index 0000000..3e0a82f --- /dev/null +++ b/inst/rstudio/addins.dcf @@ -0,0 +1,9 @@ +Name: dso repro stage +Description: Execute dso repro -s +Binding: dso_repro_stage_addin +Interactive: false + +Name: dso repro stage with dependencies +Description: Execute dso repro +Binding: dso_repro_stage_w_dependencies_addin +Interactive: false diff --git a/man/dso_repro_stage_addin.Rd b/man/dso_repro_stage_addin.Rd new file mode 100644 index 0000000..1bc532c --- /dev/null +++ b/man/dso_repro_stage_addin.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/addin.R +\name{dso_repro_stage_addin} +\alias{dso_repro_stage_addin} +\title{Execute dso repro and display the result} +\usage{ +dso_repro_stage_addin() +} +\value{ +None +} +\description{ +This function reproduces the current stage without its dependencies +and displays the resulting report in the RStudio Viewer pane. +} +\examples{ +\dontrun{ +dso_repro_stage_addin() +} +} diff --git a/man/dso_repro_stage_w_dependencies_addin.Rd b/man/dso_repro_stage_w_dependencies_addin.Rd new file mode 100644 index 0000000..d785849 --- /dev/null +++ b/man/dso_repro_stage_w_dependencies_addin.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/addin.R +\name{dso_repro_stage_w_dependencies_addin} +\alias{dso_repro_stage_w_dependencies_addin} +\title{Execute dso repro with dependencies and display the result} +\usage{ +dso_repro_stage_w_dependencies_addin() +} +\value{ +None +} +\description{ +This function reproduces the current stage along its dependencies +and displays the resulting report in the RStudio Viewer pane. +} +\examples{ +\dontrun{ +dso_repro_stage_w_dependencies_addin() +} +} diff --git a/man/repro.Rd b/man/repro.Rd new file mode 100644 index 0000000..4de3c64 --- /dev/null +++ b/man/repro.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/repro.R +\name{repro} +\alias{repro} +\title{Reproduce a dso stage} +\usage{ +repro(stage_path = stage_here(), single_stage = F) +} +\arguments{ +\item{stage_path}{The path to a stage. Defaults to the current stage by using `stage_here()`.} + +\item{single_stage}{Logical flag indicating whether to reproduce only the current stage (`TRUE`) or the current stage with all dependencies (`FALSE`). Defaults to `FALSE`.} +} +\description{ +Reproduce a dso stage with or without its dependencies. +} +\details{ +This function reproduces a stage specified by `stage_path`. + If `single_stage` is set to `TRUE`, +it reproduces the stage without its dependencies. +Otherwise, it reproduces the current stage +along with all its dependency stages. +By default, the current stage will be reproduced. +}