From 33b75d91b60eabf5d4979e181474168cc05c99f0 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Thu, 11 Feb 2021 20:54:51 -0800 Subject: [PATCH] Surface the fact that there is a local .Rprofile --- R/reprex_document.R | 75 +++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/R/reprex_document.R b/R/reprex_document.R index 830ad5ed..00b23789 100644 --- a/R/reprex_document.R +++ b/R/reprex_document.R @@ -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( @@ -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") { @@ -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")) {