Skip to content

Commit

Permalink
have undo functions always open up a file containing the clean r code
Browse files Browse the repository at this point in the history
  • Loading branch information
crew102 committed Oct 2, 2018
1 parent b65f0ce commit 5c79f2f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
26 changes: 14 additions & 12 deletions R/reprex-undo.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#' interpreted as the path to a file containing reprex code. Otherwise,
#' assumed to hold reprex code as character vector. If not provided, the
#' clipboard is consulted for input.
#' @param outfile Optional basename for output file. When `NULL`, no file is
#' left behind. If `outfile = "foo"`, expect an output file in current working
#' @param outfile Optional basename for output file. When `NULL`
#' (default), reprex writes to temp files below the session temp directory.
#' If `outfile = "foo"`, expect an output file in current working
#' directory named `foo_clean.R`. If `outfile = NA`, expect on output file in
#' a location and with basename derived from `input`, if a path, or in
#' current working directory with basename derived from [tempfile()]
Expand Down Expand Up @@ -157,14 +158,11 @@ reprex_undo <- function(input = NULL,
)
comment <- arg_option(comment)

outfile_given <- !is.null(outfile)
infile <- if (where == "path") input else NULL
if (outfile_given) {
files <- make_filenames(make_filebase(outfile, infile), suffix = "clean")
r_file <- files[["r_file"]]
if (would_clobber(r_file)) {
return(invisible())
}
files <- make_filenames(make_filebase(outfile, infile), suffix = "clean")
r_file <- files[["r_file"]]
if (would_clobber(r_file)) {
return(invisible())
}

if (is_md) { ## reprex_invert
Expand All @@ -184,10 +182,14 @@ reprex_undo <- function(input = NULL,
clipr::write_clip(x_out)
message("Clean code is on the clipboard.")
}
if (outfile_given) {
writeLines(x_out, r_file)
message("Writing clean code as R script:\n * ", r_file)

writeLines(x_out, r_file)
message("Writing clean code as R script:\n * ", r_file)

if (interactive()) {
withr::defer(file_edit(r_file))
}

invisible(x_out)
}

Expand Down
9 changes: 4 additions & 5 deletions man/reprex.Rd

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

13 changes: 7 additions & 6 deletions man/un-reprex.Rd

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

0 comments on commit 5c79f2f

Please sign in to comment.