Skip to content

Commit

Permalink
updated addin; included repro function
Browse files Browse the repository at this point in the history
  • Loading branch information
DSchreyer committed Nov 6, 2024
1 parent 9171db1 commit f942af1
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 75 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### New Features

- Introduced the rstudio addin to `dso repro` the current stage
- Introduced the rstudio addin to `dso repro` the current stage with dependencies
- 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

Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ 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)
Expand Down
130 changes: 58 additions & 72 deletions R/addin.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Execute dso repro and display the result
#'
#' This function runs the `dso repro` command on the specified `dvc.yaml` file
#' This function reproduces the current stage without its dependencies
#' and displays the resulting report in the RStudio Viewer pane.
#'
#' @return None
Expand All @@ -11,85 +11,71 @@
#' \dontrun{
#' dso_repro_stage_addin()
#' }
#' @export
dso_repro_stage_addin <- function() {
tryCatch(
{
if (length(stage_here()) == 0) {
stop(glue::glue("stage_here() is not defined. Please read in your config file using read_params()."))
} else {
stage_path <- stage_here()
}

dvc_yaml_path <- file.path(stage_path, "dvc.yaml")

message(glue::glue("Reproducing the current stage"))
result <- system2(
DSO_EXEC,
c("repro -s", shQuote(dvc_yaml_path))
)
if (result != 0) {
stop(glue::glue("DSO repro -s failed with status: {result}"))
} else {
message("System command executed successfully")
}
stage_path <- get_stage_path()
dvc_yaml_path <- file.path(stage_path, "dvc.yaml")

report_files <- list.files(stage_here("report"), pattern = "\\.pdf$|\\.html$", full.names = TRUE)
repro(dvc_yaml_path, single_stage = TRUE)

if (length(report_files) == 1) {
report_file <- report_files[1]
message(glue::glue("Report generated: {report_file}"))
message(glue::glue("Displaying report file: {report_file}"))
# Check the file extension and display in the viewer
rstudioapi::viewer(report_file)
} else {
stop(glue::glue("No report file or multiple report files were identified. Please check the report directory."))
}
},
error = function(e) {
message(glue::glue("An error occurred: {e$message}"))
}
)
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() {
tryCatch(
{
if (length(stage_here()) == 0) {
stop(glue::glue("stage_here() is not defined. Please read in your config file using read_params()."))
} else {
stage_path <- stage_here()
}
stage_path <- get_stage_path()
dvc_yaml_path <- file.path(stage_path, "dvc.yaml")

repro(dvc_yaml_path, single_stage = FALSE)

dvc_yaml_path <- file.path(stage_path, "dvc.yaml")
check_report()
}

message(glue::glue("Reproducing the current stage with all its dependency stages."))

result <- system2(
DSO_EXEC,
c("repro", shQuote(dvc_yaml_path))
)

if (result != 0) {
stop(glue::glue("DSO repro -s failed with status: {result}"))
} else {
message("System command executed successfully")
}
#' 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
get_stage_path <- function() {
if (length(stage_here()) == 0) {
stop(glue::glue("stage_here() is not defined. Please read in your config file using read_params()."))
} else {
return(stage_here())
}
}

report_files <- list.files(stage_here("report"), pattern = "\\.pdf$|\\.html$", full.names = TRUE)
#' Check and display the report
#'
#' This function checks for the generated report files in the stage directory
#' and displays the report in the RStudio Viewer pane if a single report file is found.
#'
#' @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}"))
message(glue::glue("Displaying report file: {report_file}"))
# Check the file extension and display in the viewer
rstudioapi::viewer(report_file)
} else {
stop(glue::glue("No report file or multiple report files were identified. Please check the report directory."))
}
},
error = function(e) {
message(glue::glue("An error occurred: {e$message}"))
}
)
if (length(report_files) == 1) {
report_file <- report_files[1]
message(glue::glue("Report generated: {report_file}"))
message(glue::glue("Displaying report file: {report_file}"))
# Check the file extension and display in the viewer
rstudioapi::viewer(report_file)
} else {
stop(glue::glue("No report file or multiple report files were identified. Please check the report directory."))
}
}
30 changes: 30 additions & 0 deletions R/repro.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' @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")
}
}

2 changes: 1 addition & 1 deletion man/dso_repro_stage_addin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/dso_repro_stage_w_dependencies_addin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions man/repro.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f942af1

Please sign in to comment.