Skip to content

Commit

Permalink
Capture stdout and stderr; fixes #90
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Sep 24, 2017
1 parent 8f89c5e commit 947c059
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
22 changes: 18 additions & 4 deletions R/reprex.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
#' @param tidyverse_quiet Logical. Sets the option `tidyverse.quiet`, which
#' suppresses (`TRUE`, the default) or includes (`FALSE`) the startup message
#' for the tidyverse package.
#' @param std_out_err Logical. Whether to append a section for output sent to
#' stdout and stderr by the reprex rendering process. This can be necessary to
#' reveal output if the reprex spawns child processes or `system()` calls. Note
#' this cannot be properly interleaved with output from the main R process, nor
#' is there any guarantee that the lines from standard output and standard
#' error are in correct chronological order. See [callr::r_safe()] for more.
#'
#' @return Character vector of rendered reprex, invisibly.
#' @examples
Expand Down Expand Up @@ -187,7 +193,7 @@ reprex <- function(
x = NULL, venue = c("gh", "so", "ds", "r"), si = FALSE, show = TRUE,
input = NULL, outfile = NULL,
comment = "#>", opts_chunk = NULL, opts_knit = NULL,
tidyverse_quiet = TRUE) {
tidyverse_quiet = TRUE, std_out_err = FALSE) {

venue <- tolower(venue)
venue <- match.arg(venue)
Expand Down Expand Up @@ -231,6 +237,11 @@ reprex <- function(
r_file <- strip_ext(outfile) %||% tempfile() ## foo or foo.md --> foo
r_file <- add_suffix(r_file, "reprex") ## foo --> foo_reprex
r_file <- add_ext(r_file) ## foo_reprex.R
if (isTRUE(std_out_err)) {
std_out_err <- gsub("_reprex.R", "_std_out_err.txt", r_file)
} else {
std_out_err <- FALSE
}

if (file.exists(r_file) &&
yesno("Oops, file already exists:\n * ", r_file, "\n",
Expand All @@ -253,6 +264,7 @@ reprex <- function(
user_opts_chunk = opts_chunk,
user_opts_knit = opts_knit,
tidyverse_quiet = as.character(tidyverse_quiet),
std_out_err = std_out_err,
chunk_tidy = prep_tidy(expr_input),
body = paste(the_source, collapse = "\n")
))
Expand All @@ -263,7 +275,7 @@ reprex <- function(
}

message("Rendering reprex...")
output_file <- md_file <- reprex_(r_file)
output_file <- md_file <- reprex_(r_file, std_out_err)
if (outfile_given) {
pathstem <- path_stem(r_file, md_file)
message("Writing reprex markdown:\n * ", sub(pathstem, "", md_file))
Expand Down Expand Up @@ -317,12 +329,14 @@ reprex <- function(
invisible(output_lines)
}

reprex_ <- function(input) {
reprex_ <- function(input, std_out_err = NULL) {
callr::r_safe(
function(input) {
rmarkdown::render(input, quiet = TRUE)
},
args = list(input = input),
spinner = interactive()
spinner = interactive(),
stdout = std_out_err,
stderr = std_out_err
)
}
6 changes: 6 additions & 0 deletions inst/templates/REPREX.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ knitr::opts_chunk$set(tidy = TRUE, tidy.opts = list(indent = 2))
#+ reprex-body
{{{body}}}

{{#std_out_err}}
#' standard output and standard error
#+ std-out-err, echo = FALSE
cat(readLines("{{{std_out_err}}}"), sep = "\n")
{{/std_out_err}}

{{#si}}
{{{si_start}}}
{{#devtools}}
Expand Down
10 changes: 9 additions & 1 deletion man/reprex.Rd

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

0 comments on commit 947c059

Please sign in to comment.