Skip to content

Commit

Permalink
Surface the fact that there is a local .Rprofile
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Feb 12, 2021
1 parent 4b9d578 commit 33b75d9
Showing 1 changed file with 45 additions and 30 deletions.
75 changes: 45 additions & 30 deletions R/reprex_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,30 @@ reprex_document <- function(venue = c("gh", "r", "rtf", "html", "slack", "so", "
}
)

pre_knit <- NULL
if (isTRUE(std_out_err) || isTRUE(advertise) || isTRUE(session_info)) {
pre_knit <- function(input, ...) {

# I don't know why the pre_knit hook operates on the **original** input
# instead of the to-be-knitted (post-spinning) input, but I need to
# operate on the latter. So I brute force the correct path.
# This is a no-op if input starts as `.Rmd`.
knit_input <- sub("[.]R$", ".spin.Rmd", input)
input_lines <- read_lines(knit_input)

if (isTRUE(advertise)) {
input_lines <- c(input_lines, "", ad(venue))
}

if (isTRUE(std_out_err)) {
input_lines <- c(input_lines, "", std_out_err_stub(input, venue))
}

if (isTRUE(session_info)) {
input_lines <- c(input_lines, "", si(venue))
}

write_lines(input_lines, knit_input)
pre_knit <- function(input, ...) {

# I don't know why the pre_knit hook operates on the **original** input
# instead of the to-be-knitted (post-spinning) input, but I need to
# operate on the latter. So I brute force the correct path.
# This is a no-op if input starts as `.Rmd`.
knit_input <- sub("[.]R$", ".spin.Rmd", input)
input_lines <- read_lines(knit_input)

input_lines <- c(rprofile_alert(venue), "", input_lines)

if (isTRUE(advertise)) {
input_lines <- c(input_lines, "", ad(venue))
}

if (isTRUE(std_out_err)) {
input_lines <- c(input_lines, "", std_out_err_stub(input, venue))
}

if (isTRUE(session_info)) {
input_lines <- c(input_lines, "", si(venue))
}

write_lines(input_lines, knit_input)
}

format <- rmarkdown::output_format(
Expand All @@ -136,13 +135,20 @@ reprex_document <- function(venue = c("gh", "r", "rtf", "html", "slack", "so", "
format
}

std_out_err_stub <- function(input, venue = "gh") {
txt <- backtick(std_file(input))
if (venue %in% c("gh", "html")) {
details(txt, desc = "Standard output and standard error")
} else { # venue %in% c("r", "rtf", "slack")
c("#### Standard output and error", txt)
rprofile_alert <- function(venue = "gh") {
if (venue %in% c("gh", "html", "slack")) {
fmt <- '"*Local `.Rprofile` detected at `%s`*"'
} else { # venue %in% c("r", "rtf")
fmt <- '"Local .Rprofile detected at %s"'
}
include_eval <-
"include = file.exists('.Rprofile'), eval = file.exists('.Rprofile')"

c(
glue::glue("```{{r, results = 'asis', echo = FALSE, {include_eval}}}"),
glue::glue('cat(sprintf({fmt}, normalizePath(".Rprofile")))'),
"```"
)
}

ad <- function(venue = "gh") {
Expand All @@ -158,6 +164,15 @@ ad <- function(venue = "gh") {
}
}

std_out_err_stub <- function(input, venue = "gh") {
txt <- backtick(std_file(input))
if (venue %in% c("gh", "html")) {
details(txt, desc = "Standard output and standard error")
} else { # venue %in% c("r", "rtf", "slack")
c("#### Standard output and error", txt)
}
}

si <- function(venue = "gh") {
txt <- r_chunk(session_info_string())
if (venue %in% c("gh", "html")) {
Expand Down

0 comments on commit 33b75d9

Please sign in to comment.