Skip to content

Commit

Permalink
mergin in main branch
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'origin/main'
  • Loading branch information
Matthew Whalen authored and Matthew Whalen committed May 19, 2023
2 parents 7bd8229 + 4fa323d commit 036f470
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ S3method(execute,sewage_joiner)
S3method(execute,sewage_node)
S3method(execute,sewage_splitter)
S3method(print,sewage_pipeline)
S3method(pull_output,sewage_pipeline)
export(Joiner)
export(Pipeline)
export(Splitter)
export(add_node)
export(draw)
export(execute)
export(pull_output)
export(run)
importFrom(glue,glue)
32 changes: 31 additions & 1 deletion R/pipeline.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ is_pipeline = function(x) {
inherits(x, 'sewage_pipeline')
}

' Run a pipeline
#' Run a pipeline
#'
#' This function is the extry point for executing a pipeline object
#' @param pipeline an initialized pipeline object
Expand Down Expand Up @@ -54,3 +54,33 @@ is_executed_pipeline = function(x) {

return(length(x$outputs) > 0)
}

#' Extract output components from a pipeline
#' @param x an executed pipeline object
#' @param component a character string specifying which output component to pull
#' #' \dontrun{
#' pipeline = Pipeline()
#' pipeline = pipeline |>
#' add_node(name = 'Splitter', component = Splitter(), input = 'file')
#' result = run(pipeline, file = mtcars)
#' result$outputs$Splitter.output_1
#' result$outputs$Splitter.output_2
#' }
#' @export
pull_output = function(x, component, ...) {
UseMethod("pull_output")
}

#' @export
#' @rdname pull_output
pull_output.sewage_pipeline = function(x, component, ...) {
if(!is_executed_pipeline(x)) {
stop("No outputs available. Please execute pipeline using 'run'")
}
if(!is.character(component)) {
stop("component must be a character string")
}
output = x$outputs[[component]]
print(output)
}

4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ print(result)
We can now access the results of our terminating nodes. A terminating node is any node that is not specified as input. By default when the pipeline is run, each node will overwrite the output of its input node. Therefore any node that is not fed forward to a new node will return output. In the case of this pipeline, the `Subsetter` and `Summarizer` edges are our terminating nodes. Therefore, we can access their results in the `outputs` object of the pipeline

```{r}
result$outputs$Subsetter
pull_output(result, "Subsetter")
```

```{r}
result$outputs$Summarizer
pull_output(result, "Summarizer")
```

## Multiple Entrypoints
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ edges are our terminating nodes. Therefore, we can access their results
in the `outputs` object of the pipeline

``` r
result$outputs$Subsetter
pull_output(result, "Subsetter")
#> # A tibble: 7 × 12
#> ...1 mpg cyl disp hp drat wt qsec vs am gear carb
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
Expand All @@ -141,7 +141,7 @@ result$outputs$Subsetter
```

``` r
result$outputs$Summarizer
pull_output(result, "Summarizer")
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 71.1 120.8 196.3 230.7 326.0 472.0
```
Expand Down
27 changes: 27 additions & 0 deletions man/pull_output.Rd

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

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

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

18 changes: 18 additions & 0 deletions tests/testthat/test-pipeline.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,21 @@ test_that("run fails for non-pipelines", {
regexp = "pipeline object must be of type 'sewage_pipeline'"
)
})

test_that("pull_output works for executed pipeline objects", {
input = "x"
pipeline = Pipeline() |>
add_node(component = c, name = "Printer", input = "x")
result = run(pipeline, x = input)
expect_identical(
pull_output(result, "Printer"),
c(input)
)
})

test_that("pull_output fails for non-executed pipelines", {
pipeline = Pipeline()
expect_error(
pull_output(pipeline, "Foo")
)
})

0 comments on commit 036f470

Please sign in to comment.