Skip to content

Commit 9c1f395

Browse files
committed
WIP
- adding get_outputs function
1 parent 79279da commit 9c1f395

File tree

6 files changed

+72
-0
lines changed

6 files changed

+72
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Collate:
6565
'qenv-get_code.R'
6666
'qenv-get_env.R'
6767
'qenv-get_messages.r'
68+
'qenv-get_outputs.R'
6869
'qenv-get_var.R'
6970
'qenv-get_warnings.R'
7071
'qenv-join.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export(eval_code)
1717
export(get_code)
1818
export(get_env)
1919
export(get_messages)
20+
export(get_outputs)
2021
export(get_var)
2122
export(get_warnings)
2223
export(join)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# teal.code 0.6.1.9003
22

3+
### Enhancements
4+
5+
* Introduced `get_outputs` function to fetch objects which have been printed or plotted in the `qenv` code.
6+
37
### Bug fixes
48

59
* Fix a problem detecting co-occurrences when expression has multiple lines.

R/qenv-get_outputs.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#' Get outputs
2+
#'
3+
#' `eval_code` evaluates code silently so plots and prints don't show up in the console or graphic devices.
4+
#' If one wants to use an output outside of the qenv (e.g. use a graph in `renderPlot`) then use `get_outputs`.
5+
#' @param object (`qenv`)
6+
#' @return list of outputs generated in a qenv
7+
#' @examples
8+
#' q <- eval_code(
9+
#' qenv(),
10+
#' quote({
11+
#' a <- 1
12+
#' print("I'm an output")
13+
#' plot(1)
14+
#' })
15+
#' )
16+
#' get_outputs(q)
17+
#' @export
18+
setGeneric("get_outputs", function(object) standardGeneric("get_outputs"))
19+
20+
21+
setMethod("get_outputs", signature = "qenv", function(object) {
22+
Reduce(
23+
function(x, y) {
24+
c(x, attr(y, "outputs"))
25+
},
26+
init = list(),
27+
x = object@code
28+
)
29+
})

man/get_outputs.Rd

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-get_outputs.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
testthat::test_that("", {
2+
q <- qenv()
3+
q1 <- eval_code(q, expression(iris, mtcars))
4+
testthat::expect_identical(
5+
get_outputs(q1),
6+
list(iris, mtcars)
7+
)
8+
})

0 commit comments

Comments
 (0)