Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: setup pre-commit and handle linting errors #126

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
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
26 changes: 14 additions & 12 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
^whirl\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
^_pkgdown\.yml$
^docs$
^pkgdown$
^README\.Rmd$
^\.lintr$
^\.Rproj\.user$
^\.github$
^inst/output$
^\.lintr$
^\.pre-commit-config\.yaml$
^_pkgdown\.yml$
^dev/
^docs$
^inst/examples/*\\.html$
^inst/examples/*_files$
^dev/
summary.html
plot1.png
^whirl\.Rcheck$
^inst/output$
^pkgdown$
^vignettes/articles$
^whirl.*\.tar\.gz$
^whirl.*\.tgz$
^vignettes/articles$
^whirl\.Rcheck$
^whirl\.Rproj$
^înst/WORDLIST$
plot1.png
summary.html
3 changes: 3 additions & 0 deletions .github/workflows/check_and_co.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
branches:
- main
- master
permissions:
contents: write
pull-requests: write
name: All actions
jobs:
check-current-version:
Expand Down
90 changes: 90 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# All available hooks: https://pre-commit.com/hooks.html
# R specific hooks: https://github.com/lorenzwalthert/precommit
repos:
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.4.3.9003
hooks:
# - id: style-files
# args: [--style_pkg=styler, --style_fun=tidyverse_style]
- id: roxygenize
additional_dependencies:
- checkmate
- dplyr
- ggplot2
- httr
- jsonlite
- kableExtra
- options
- quarto
- rmarkdown
- sessioninfo
- tibble
- tidyr
- unglue
- NovoNordisk-OpenSource/zephyr
- reticulate
- id: use-tidy-description
- id: spell-check
exclude: >
(?x)^(
.*\.[rR]|
.*\.feather|
.*\.jpeg|
.*\.pdf|
.*\.png|
.*\.py|
.*\.RData|
.*\.rds|
.*\.Rds|
.*\.Rproj|
.*\.sh|
(.*/|)\.gitignore|
(.*/|)\.gitlab-ci\.yml|
(.*/|)\.lintr|
(.*/|)\.pre-commit-.*|
(.*/|)\.Rbuildignore|
(.*/|)\.Renviron|
(.*/|)\.Rprofile|
(.*/|)\.travis\.yml|
(.*/|)appveyor\.yml|
(.*/|)NAMESPACE|
(.*/|)renv/settings\.dcf|
(.*/|)renv\.lock|
(.*/|)WORDLIST|
\.github/workflows/.*|
data/.*|
)$
# - id: lintr
- id: readme-rmd-rendered
- id: parsable-R
- id: no-browser-statement
# - id: no-print-statement
- id: no-debug-statement
- id: deps-in-desc
- id: pkgdown
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-added-large-files
args: ['--maxkb=200']
- id: file-contents-sorter
files: '^\.Rbuildignore$'
- id: end-of-file-fixer
exclude: '\.Rd'
- repo: https://github.com/pre-commit-ci/pre-commit-ci-config
rev: v1.6.1
hooks:
# Only required when https://pre-commit.ci is used for config validation
- id: check-pre-commit-ci-config
- repo: local
hooks:
- id: forbid-to-commit
name: Don't commit common R artifacts
entry: Cannot commit .Rhistory, .RData, .Rds or .rds.
language: fail
files: '\.(Rhistory|RData|Rds|rds)$'
# `exclude: <regex>` to allow committing specific files

ci:
autoupdate_schedule: monthly
skip: [pkgdown]
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: whirl
Title: Logging package
Version: 0.1.7
Version: 0.1.7.9000
Authors@R: c(
person("Aksel", "Thomsen", , "[email protected]", role = c("aut", "cre")),
person("Lovemore", "Gakava", , "[email protected]", role = "aut"),
Expand All @@ -10,7 +10,7 @@ Authors@R: c(
person("Vladimir", "Obucina", , email = "[email protected]", role = "aut"),
person("Novo Nordisk A/S", role = "cph")
)
Description: Provides functionalities for running R scripts in batch, while simultanously creating logs for each script execution.
Description: Provides functionalities for running R scripts in batch, while simultaneously creating logs for each script execution.
License: Apache License (>= 2)
URL: https://novonordisk-opensource.github.io/whirl/, https://github.com/novonordisk-opensource/whirl
Depends:
Expand Down Expand Up @@ -47,7 +47,7 @@ Suggests:
testthat (>= 3.0.0),
usethis
Remotes:
NovoNordisk-OpenSource/zephyr
NovoNordisk-OpenSource/zephyr@a4d5163108e2c4042f98bd00f17f1af7353b08c2
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# whirl dev
* Added pre-commit for developers
* Fixed linting errors

# whirl 0.1.7 (2024-12-17)
* Enable redirection of logs through the `log_dir` argument in `run()`.
* Changed the title on the individual logs to the script name and moved the path to a distinct section within the title-block.
Expand Down
63 changes: 52 additions & 11 deletions R/approvedpkgs.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' An utility function to help you build your approved packages .
#' @noRd
check_approved <- function(approved_pkg_folder,

Check warning on line 5 in R/approvedpkgs.R

View workflow job for this annotation

GitHub Actions / Megalinter / MegaLinter

file=/github/workspace/R/approvedpkgs.R,line=5,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 22.
approved_pkg_url,
session_pkgs,
output_file = NULL) {
Expand Down Expand Up @@ -34,7 +34,10 @@
) |>
dplyr::select("Package", "Version", "Repository")
session_pkgs |>
dplyr::left_join(src_url, by = c("package" = "Package", "loadedversion" = "Version")) |>
dplyr::left_join(
y = src_url,
by = c("package" = "Package", "loadedversion" = "Version")
) |>
dplyr::mutate(
Approved = ifelse(is.na(.data[["Repository"]]), "No", "Yes"),
"Approved Repository" = url
Expand All @@ -56,7 +59,10 @@
) |>
dplyr::select("Package", "Version", "Repository")
session_pkgs |>
dplyr::left_join(src_file, by = c("package" = "Package", "loadedversion" = "Version")) |>
dplyr::left_join(
y = src_file,
by = c("package" = "Package", "loadedversion" = "Version")
) |>
dplyr::mutate(
Approved = ifelse(is.na(.data[["Repository"]]), "No", "Yes"),
"Approved Repository" = folder
Expand All @@ -69,17 +75,30 @@

if (is.null(approved_pkg_folder)) {
approved_dset <- approved_dset_url |>
dplyr::select("package", "loadedversion", "date", "source", "Approved", "Approved Repository") |>
dplyr::select(
"package", "loadedversion", "date", "source", "Approved",
"Approved Repository"
) |>
dplyr::rename("Repository URL" = "Approved Repository") |>
dplyr::arrange(.data[["Approved"]], .data[["package"]])
} else if (is.null(approved_pkg_url) || length(approved_pkg_url) == 0) {
approved_dset <- approved_dset_file |>
dplyr::select("package", "loadedversion", "date", "source", "Approved", "Approved Repository") |>
dplyr::select(
"package", "loadedversion", "date", "source", "Approved",
"Approved Repository"
) |>
dplyr::rename("Repository Folder" = "Approved Repository") |>
dplyr::arrange(.data[["Approved"]], .data[["package"]])
} else {
approved_dset <- dplyr::full_join(approved_dset_url, approved_dset_file, by = c("package", "loadedversion")) |>
dplyr::select("package", "loadedversion", "date.x", "source.x", "Approved.x", "Approved Repository.x", "Approved.y", "Approved Repository.y") |>
approved_dset <- dplyr::full_join(
x = approved_dset_url,
y = approved_dset_file,
by = c("package", "loadedversion")
) |>
dplyr::select(
"package", "loadedversion", "date.x", "source.x", "Approved.x",
"Approved Repository.x", "Approved.y", "Approved Repository.y"
) |>
dplyr::rename(
"date" = "date.x",
"source" = "source.x",
Expand All @@ -88,7 +107,11 @@
"Approved in Repository URL" = "Approved.x",
"Approved in Repository Folder" = "Approved.y"
) |>
dplyr::arrange(.data[["Approved in Repository URL"]], .data[["Approved in Repository Folder"]], .data[["package"]])
dplyr::arrange(
.data[["Approved in Repository URL"]],
.data[["Approved in Repository Folder"]],
.data[["package"]]
)
}

if (is.null(output_file)) {
Expand All @@ -103,18 +126,36 @@
create_approval_plot <- function(data) {
row.names(data) <- NULL

data$grpvar <- ifelse(rowSums(as.matrix(data[, grepl("^Approved", colnames(data))]) == "No") == ncol(as.matrix(data[, grepl("^Approved", colnames(data))])), "No", "Yes")
data$grpvar <- ifelse(
rowSums(as.matrix(data[, grepl("^Approved", colnames(data))]) == "No") ==
ncol(as.matrix(data[, grepl("^Approved", colnames(data))])),
"No", "Yes"
)

data |>
dplyr::count(.data[["grpvar"]]) |>
dplyr::mutate(
pct = prop.table(.data[["n"]]),
status = "grpvar",
lbl = paste0(.data[["grpvar"]], ": ", .data[["n"]], "/", sum(.data[["n"]]), " (", scale_to_percent(.data[["pct"]]), ")")
lbl = paste0(
.data[["grpvar"]], ": ",
.data[["n"]], "/",
sum(.data[["n"]]), " (",
scale_to_percent(.data[["pct"]]), ")"
)
) |>
ggplot2::ggplot(ggplot2::aes(x = .data[["pct"]], y = .data[["status"]], fill = .data[["grpvar"]], label = .data[["lbl"]])) +
ggplot2::ggplot(
mapping = ggplot2::aes(
x = .data[["pct"]],
y = .data[["status"]],
fill = .data[["grpvar"]],
label = .data[["lbl"]]
)
) +
ggplot2::geom_bar(position = "fill", stat = "identity") +
ggplot2::geom_text(position = ggplot2::position_stack(vjust = 0.5, reverse = FALSE)) +
ggplot2::geom_text(
position = ggplot2::position_stack(vjust = 0.5, reverse = FALSE)
) +
ggplot2::theme_void() +
ggplot2::theme(
legend.position = "none",
Expand Down
19 changes: 8 additions & 11 deletions R/custom_logging.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
#' Useful for e.g. read and write operations on databases etc.
#' that are not automatically captured.
#'
#' The default environment variable `WHIRL_LOG_MSG` is set in the session used to log scripts, and input
#' is automatically captured in the resulting log.
#' The default environment variable `WHIRL_LOG_MSG` is set in the session used
#' to log scripts, and input is automatically captured in the resulting log.
#'
#' If run outside of whirl, meaning when the above environment variable is unset, the operations
#' are streamed to `stdout()`. By default the console.
#' If run outside of whirl, meaning when the above environment variable is
#' unset, the operations are streamed to `stdout()`. By default the console.
#'
#' @name custom_logging
#' @param file [character()] description of the file that was read, written or deleted.
#' @param file [character()] description of the file that was read, written or
#' deleted.
#' @param log [character()] path to the log file.
NULL

Expand All @@ -37,17 +38,15 @@ write_to_log <- function(
file,
type = c("read", "write", "delete"),
log = Sys.getenv("WHIRL_LOG_MSG")) {

type <- rlang::arg_match(type)
checkmate::assert_string(type)
checkmate::assert_string(file)
checkmate::assert_string(log)
time <- Sys.time()

x <- log_df(
type = type,
file = file
)
)

if (log == "") {
jsonlite::stream_out(x = x, verbose = FALSE)
Expand All @@ -60,7 +59,6 @@ write_to_log <- function(

#' @noRd
read_from_log <- function(log = Sys.getenv("WHIRL_LOG_MSG")) {

if (log == "" || !file.exists(log)) {
return(log_df())
}
Expand All @@ -83,7 +81,6 @@ log_df <- function(type = character(), file = character()) {

#' @noRd
split_log <- function(log_df, types = c("read", "write", "delete")) {

class(log_df) <- c("whirl_log_info", class(log_df))

# Split in a tibble for each type of output
Expand All @@ -108,7 +105,7 @@ split_log <- function(log_df, types = c("read", "write", "delete")) {
}

#' @noRd
knit_print.whirl_log_info <- function(x, ...) {
knit_print.whirl_log_info <- function(x, ...) { # nolint
x |>
knitr::kable(
row.names = FALSE
Expand Down
Loading
Loading