Skip to content
Open
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
2 changes: 0 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ Imports:
sessioninfo (>= 1.2),
tibble,
digest,
lifecycle,
methods,
yaml
Suggests:
testthat (>= 3.0.0),
Expand Down
3 changes: 1 addition & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# logrx (development version)

## New Features

- Added `error_on_unapproved` parameter to `axecute()` to optionally stop execution
if unapproved packages are detected before running the script (#204)
- Parses the script's AST to detect packages from `library()`/`require()` calls
Expand All @@ -19,7 +18,7 @@
- `yaml` has been moved from `Suggests` to `Imports`

## Updates

- Removed the `remove_log_object` argument from `log_write()` (#298).
- Improved error messaging when a log.rx environment already exists (#276)
- In non-interactive mode, provides clear guidance on how to resolve the issue
- In interactive mode, offers users the option to automatically remove the existing environment or handle it manually
Expand Down
58 changes: 29 additions & 29 deletions R/axecute.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,18 @@
#' close(fileConn)
#'
#' axecute(file.path(dir, "hello.Rmd"))
axecute <- function(file,
log_name = NA,
log_path = NA,
include_rds = FALSE,
quit_on_error = TRUE,
to_report = c("messages", "output", "result"),
show_repo_url = FALSE,
error_on_unapproved = FALSE,
extra_info = NA,
...) {
# deprecations
if (methods::hasArg(remove_log_object)) {
lifecycle::deprecate_stop("0.3.0", "axecute(remove_log_object = )", "axecute(include_rds = )")
}

# remove log object
remove_log_object <- TRUE

axecute <- function(
file,
log_name = NA,
log_path = NA,
include_rds = FALSE,
quit_on_error = TRUE,
to_report = c("messages", "output", "result"),
show_repo_url = FALSE,
error_on_unapproved = FALSE,
extra_info = NA,
...
) {
# lower everything for consistency and check values
to_report <- map_chr(to_report, tolower)
match.arg(to_report, several.ok = TRUE)
Expand All @@ -104,18 +98,25 @@ axecute <- function(file,
if (!is.null(unapproved_pkgs)) {
set_log_element("hash_sum", digest::sha1(readLines(file, warn = FALSE)))
set_log_element("session_info", get_session_info())
set_log_element("errors", list(message = paste0(
"Unapproved packages detected prior to execution: ",
paste(unapproved_pkgs, collapse = ", ")
)))
set_log_element("unapproved_packages_functions", data.frame(
function_name = "",
library = paste0("package:", unapproved_pkgs),
stringsAsFactors = FALSE
))
set_log_element(
"errors",
list(
message = paste0(
"Unapproved packages detected prior to execution: ",
paste(unapproved_pkgs, collapse = ", ")
)
)
)
set_log_element(
"unapproved_packages_functions",
data.frame(
function_name = "",
library = paste0("package:", unapproved_pkgs),
stringsAsFactors = FALSE
)
)
log_write(
file = file,
remove_log_object = TRUE,
show_repo_url = show_repo_url,
include_rds = include_rds,
to_report = character(0),
Expand All @@ -138,7 +139,6 @@ axecute <- function(file,
# write log
log_write(
file = file,
remove_log_object = remove_log_object,
show_repo_url = show_repo_url,
include_rds = include_rds,
to_report = to_report,
Expand Down
73 changes: 46 additions & 27 deletions R/log.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ log_init <- function() {
#'
#' # Write the log
#' log_write(file)
log_config <- function(file = NA,
log_name = NA,
log_path = NA,
extra_info = NA) {
log_config <- function(
file = NA,
log_name = NA,
log_path = NA,
extra_info = NA
) {
# If the log.rx environment is not NULL or empty, handle it appropriately
if (!is.null(getOption("log.rx"))) {
if (!(identical(ls(getOption("log.rx")), character(0)))) {
Expand Down Expand Up @@ -233,8 +235,6 @@ log_cleanup <- function() {
#' @param file String. Path to file executed
#' @param include_rds Boolean. Option to export log object as Rds file.
#' Defaults to FALSE
#' @param remove_log_object Boolean. Should the log object be removed after
#' writing the log file? Defaults to TRUE
#' @param to_report String vector. Objects to optionally report; additional
#' information in \code{\link{axecute}}
#' @param show_repo_url Boolean. Should the repo URLs be reported
Expand Down Expand Up @@ -262,12 +262,13 @@ log_cleanup <- function() {
#'
#' # Write the log
#' log_write(file)
log_write <- function(file = NA,
remove_log_object = TRUE,
show_repo_url = FALSE,
include_rds = FALSE,
extra_info = NA,
to_report = c("messages", "output", "result")) {
log_write <- function(
file = NA,
show_repo_url = FALSE,
include_rds = FALSE,
extra_info = NA,
to_report = c("messages", "output", "result")
) {
# Set end time and run time
set_log_element("end_time", strftime(Sys.time(), usetz = TRUE))
set_log_element(
Expand Down Expand Up @@ -305,7 +306,6 @@ log_write <- function(file = NA,
write_hash_sum()
)


cleaned_log_vec <- c(
cleaned_log_vec,
write_log_header("Session Information"),
Expand Down Expand Up @@ -340,7 +340,9 @@ log_write <- function(file = NA,
write_used_functions()
)

cleaned_log <- cleaned_log[!(names(cleaned_log)) %in% "used_packages_functions"]
cleaned_log <- cleaned_log[
!(names(cleaned_log)) %in% "used_packages_functions"
]
}

if (file.exists(getOption("log.rx.approved"))) {
Expand All @@ -354,7 +356,10 @@ log_write <- function(file = NA,
} else {
readRDS(approved_path)
}
unapproved_functions <- get_unapproved_use(used_functions, approved_functions)
unapproved_functions <- get_unapproved_use(
used_functions,
approved_functions
)
set_log_element("unapproved_packages_functions", unapproved_functions)
}

Expand All @@ -364,7 +369,9 @@ log_write <- function(file = NA,
write_unapproved_functions()
)

cleaned_log <- cleaned_log[!(names(cleaned_log)) %in% "unapproved_packages_functions"]
cleaned_log <- cleaned_log[
!(names(cleaned_log)) %in% "unapproved_packages_functions"
]
}

if ("lint_results" %in% names(log_cleanup())) {
Expand Down Expand Up @@ -433,17 +440,27 @@ log_write <- function(file = NA,
write_log_element("log_name", "Log name: "),
write_log_element("log_path", "Log path: ")
)
writeLines(cleaned_log_vec,
writeLines(
cleaned_log_vec,
con = file.path(
get_log_element("log_path"),
get_log_element("log_name")
)
)
if (include_rds) {
rds_fields <- c(
"end_time", "start_time", "run_time", "user", "hash_sum",
"log_path", "log_name", "file_path", "file_name",
"unapproved_packages_functions", "errors", "warnings",
"end_time",
"start_time",
"run_time",
"user",
"hash_sum",
"log_path",
"log_name",
"file_path",
"file_name",
"unapproved_packages_functions",
"errors",
"warnings",
"session_info"
)
log_options <- as.list(getOption("log.rx"))
Expand All @@ -460,18 +477,20 @@ log_write <- function(file = NA,
}
}
)
saveRDS(cleaned_log_list,
saveRDS(
cleaned_log_list,
file = file.path(
get_log_element("log_path"),
paste0(tools::file_path_sans_ext(
get_log_element("log_name")
), ".Rds")
paste0(
tools::file_path_sans_ext(
get_log_element("log_name")
),
".Rds"
)
)
)
}
if (remove_log_object) {
log_remove()
}
log_remove()
}


Expand Down
2 changes: 1 addition & 1 deletion man/axecute.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions man/log_write.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading