Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Depends:
Imports:
knitr,
readr (>= 1.0.0),
rmarkdown,
withr (>= 2.5.0)
rmarkdown
Suggests:
callr (>= 3.7.5),
pkgload,
testthat (>= 3.0.0),
usethis
usethis,
withr (>= 2.5.0)
Config/Needs/website: arnaudgallou/cygne
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pakret (development version)

* pakret now works when called after the package [conflicted](https://conflicted.r-lib.org) (#30).

* Citing multiple packages is now significantly faster (#31).

# pakret 0.2.2
Expand Down
2 changes: 1 addition & 1 deletion R/pkrt-set.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ reset <- function(x) {
update <- function(x) {
if (is_updating_bib(x)) {
bib_write()
withr::defer(bib_set())
defer(bib_set())
}
do.call(set, as.list(x))
}
Expand Down
2 changes: 1 addition & 1 deletion R/utils-local.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ bib_field <- function(name) {
}

load_pkg <- function(path, env) {
withr::defer(pkgload::unload(basename(path), quiet = TRUE), envir = env)
defer(pkgload::unload(basename(path), quiet = TRUE), frame = env)
pkgload::load_all(path, export_all = FALSE, quiet = TRUE)
}

Expand Down
20 changes: 17 additions & 3 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@
bibs <- rmarkdown::metadata$bibliography
set(render = TRUE, bibliography = bibs)
bib_init()
terminate()
defer_knitr(bib_write())
}
}

terminate <- function() {
withr::defer(bib_write(), envir = parent.frame(5L))
defer <- function(expr, frame = parent.frame()) {
thunk <- as.call(list(function() expr))
do.call(on.exit, list(thunk, TRUE, FALSE), envir = frame)
}

defer_knitr <- function(expr) {
defer(expr, knitr_exit_frame())
}

knitr_exit_frame <- function() {
ns <- asNamespace("knitr")
for (frame in as.list(sys.frames())) {
if (identical(topenv(frame), ns)) {
return(frame)
}
}
}
2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local_settings <- function(..., env = parent.frame()) {
withr::defer(reset(...names()), envir = env)
defer(reset(...names()), frame = env)
pkrt_set(...)
}

Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-pkrt.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ test_that("pakret handles references that have a premade key (#18)", {
expect_match(res, "@Manual\\{baz,")
})

test_that("pakret works with modified frame stacks (#30)", {
template <- dedent("
---
bibliography: %s
---
```{r}
(function() library(pakret))()
pakret:::load_foo()
```
`r pkrt('foo')`
")
dir <- local_files(template)
res <- read_local_file(dir, target = "bib")

expect_match(res, "@Manual\\{foo,")
})

# errors

test_that("pkrt() gives meaningful error messages", {
Expand Down