Skip to content

Commit

Permalink
Switch to desired working directory BEFORE callr::r()
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Feb 12, 2021
1 parent f570d21 commit 4b9d578
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
36 changes: 21 additions & 15 deletions R/reprex_render.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,27 @@ reprex_render_impl <- function(input,
reprex.current_venue = venue
)
if (new_session) {
out <- tryCatch(
callr::r(
function(input, opts) {
options(opts)
rmarkdown::render(
input,
quiet = TRUE, envir = globalenv(), encoding = "UTF-8"
)
},
args = list(input = input, opts = opts),
spinner = is_interactive(),
stdout = std_file,
stderr = std_file
),
error = function(e) e
# if callr::r() picks up a local .Rprofile, it should be local to
# where the the reprex work is happening, not the session where reprex()
# was called
withr::with_dir(
path_dir(input),
out <- tryCatch(
callr::r(
function(input, opts) {
options(opts)
rmarkdown::render(
input,
quiet = TRUE, envir = globalenv(), encoding = "UTF-8"
)
},
args = list(input = path_file(input), opts = opts),
spinner = is_interactive(),
stdout = if (is.null(std_file)) NULL else path_file(std_file),
stderr = if (is.null(std_file)) NULL else path_file(std_file)
),
error = function(e) e
)
)

# reprex has crashed R
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-rprofile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that(".Rprofile local to reprex target directory is consulted", {
local_temp_wd("reprextests-aaa")
cat("x <- 'aaa'\n", file = ".Rprofile")
cat("x\n", file = "foo.R")
aaa_foo <- path_abs("foo.R")

local_temp_wd("reprextests-bbb")
cat("x <- 'bbb'\n", file = ".Rprofile")

out <- reprex(x, outfile = NA, advertise = FALSE)
expect_match(out, "bbb", all = FALSE)

out <- reprex(input = aaa_foo, outfile = NA, advertise = FALSE)
expect_match(out, "aaa", all = FALSE)
})

0 comments on commit 4b9d578

Please sign in to comment.