# NOT RUN {
## a rendered reprex can be inverted, at least approximately
tmp_in <- file.path(tempdir(), "roundtrip-input")
x <- reprex({
#' Some text
#+ chunk-label-and-options-cannot-be-recovered, message = TRUE
(x <- 1:4)
#' More text
y <- 2:5
x + y
}, show = FALSE, advertise = FALSE, outfile = tmp_in)
tmp_out <- file.path(tempdir(), "roundtrip-output")
x <- reprex_invert(x, outfile = tmp_out)
x
# clean up
file.remove(
list.files(dirname(tmp_in), pattern = "roundtrip", full.names = TRUE)
)
# }
# NOT RUN {
## a displayed reprex can be cleaned of commented output
tmp <- file.path(tempdir(), "commented-code")
x <- c(
"## a regular comment, which is retained",
"(x <- 1:4)",
"#> [1] 1 2 3 4",
"median(x)",
"#> [1] 2.5"
)
out <- reprex_clean(x, outfile = tmp)
out
# clean up
file.remove(
list.files(dirname(tmp), pattern = "commented-code", full.names = TRUE)
)
## round trip with reprex(..., venue = "R")
code_in <- c("x <- rnorm(2)", "min(x)")
res <- reprex(input = code_in, venue = "R", advertise = FALSE)
res
(code_out <- reprex_clean(res))
identical(code_in, code_out)
# }
# NOT RUN {
## rescue a reprex that was copied from a live R session
tmp <- file.path(tempdir(), "live-transcript")
x <- c(
"> ## a regular comment, which is retained",
"> (x <- 1:4)",
"[1] 1 2 3 4",
"> median(x)",
"[1] 2.5"
)
out <- reprex_rescue(x, outfile = tmp)
out
# clean up
file.remove(
list.files(dirname(tmp),pattern = "live-transcript", full.names = TRUE)
)
# }
Run the code above in your browser using DataLab