Skip to content

Commit

Permalink
updated source path logic to prefer sourced file over open editor file.
Browse files Browse the repository at this point in the history
  • Loading branch information
mduncans committed Oct 31, 2024
1 parent 77b282a commit 7e5e0d8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: reportifyr
Title: Create reproducible reports with quarto and word
Version: 0.2.0
Version: 0.2.1
Authors@R: c(
person("Jacob", "Dumbleton", , "[email protected]", role = c("aut", "cre")),
person("Matthew", "Smith", , "[email protected]", role = "aut"),
Expand Down
14 changes: 9 additions & 5 deletions R/write_object_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ write_object_metadata <- function(
log4r::info(.le$logger, paste0("Generated file hash: ", hash))

source_path <- tryCatch({
if (requireNamespace("rstudioapi", quietly = TRUE) && rstudioapi::isAvailable()) {
# Check if the script is being sourced
src_path <- if (!is.null(sys.frame(1)$ofile)) {
normalizePath(sys.frame(1)$ofile) # Path of the currently sourced file
} else if (!is.null(knitr::current_input())) {
normalizePath(knitr::current_input())
} else if (requireNamespace("rstudioapi", quietly = TRUE) && rstudioapi::isAvailable()) {
context <- rstudioapi::getSourceEditorContext()
if (!is.null(context$path) && nzchar(context$path)) { # Check if the context and path are non-null and non-empty
if (!is.null(context$path) && nzchar(context$path)) {
normalizePath(context$path)
} else {
normalizePath("Object created from console") # Fallback to the working directory if no source file is open
normalizePath("Object created from console")
}
} else if (!is.null(knitr::current_input())) {
normalizePath(knitr::current_input())
} else if (!is.null(rmarkdown::metadata$input_file)) {
normalizePath(rmarkdown::metadata$input_file)
} else if (!is.null(getOption("knitr.in.file"))) {
Expand All @@ -60,6 +63,7 @@ write_object_metadata <- function(
} else {
stop("Unable to detect input file")
}
src_path
}, error = function(e) {
log4r::error(.le$logger, "Error detecting source file path")
stop(e)
Expand Down

0 comments on commit 7e5e0d8

Please sign in to comment.