From 23c571c164e929ebf50e8b268242e06c93a4681a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KTQN=20=28Kristian=20Tr=C3=B8jelsgaard=20Nielsen=29?= Date: Mon, 2 Dec 2024 12:59:56 +0100 Subject: [PATCH 1/3] Substitute "%>%" with "|>" --- DESCRIPTION | 1 - NAMESPACE | 2 -- R/connector.R | 8 +++--- R/dbi.R | 4 +-- R/dbi_methods.R | 12 ++++---- R/fs.R | 8 +++--- R/fs_methods.R | 2 +- R/utils-pipe.R | 14 ---------- R/utils_files.R | 4 +-- dev/connector_config_yaml.qmd | 6 ++-- man/pipe.Rd | 20 ------------- tests/testthat/_snaps/connect.md | 2 +- tests/testthat/test-connect.R | 16 +++++++---- vignettes/connector.Rmd | 48 ++++++++++++++++---------------- 14 files changed, 57 insertions(+), 90 deletions(-) delete mode 100644 R/utils-pipe.R delete mode 100644 man/pipe.Rd diff --git a/DESCRIPTION b/DESCRIPTION index cf65929..fe952f0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,7 +20,6 @@ Imports: glue, haven, jsonlite, - magrittr, options, purrr, R6 (>= 2.4.0), diff --git a/NAMESPACE b/NAMESPACE index 0ec5129..06cdcb1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -42,7 +42,6 @@ S3method(write_ext,txt) S3method(write_ext,xpt) S3method(write_ext,yaml) S3method(write_ext,yml) -export("%>%") export(connect) export(connector) export(connector_dbi) @@ -70,6 +69,5 @@ importFrom(cli,cli_code) importFrom(cli,cli_inform) importFrom(cli,cli_text) importFrom(jsonlite,read_json) -importFrom(magrittr,"%>%") importFrom(options,define_option) importFrom(rlang,set_names) diff --git a/R/connector.R b/R/connector.R index fb22043..7769d7a 100644 --- a/R/connector.R +++ b/R/connector.R @@ -65,7 +65,7 @@ connector <- R6::R6Class( #' List available content from the connector. See also [list_content_cnt]. #' @return A [character] vector of content names list_content_cnt = function(...) { - self %>% + self |> list_content_cnt(...) }, @@ -74,7 +74,7 @@ connector <- R6::R6Class( #' @return #' R object with the content. For rectangular data a [data.frame]. read_cnt = function(name, ...) { - self %>% + self |> read_cnt(name, ...) }, @@ -82,7 +82,7 @@ connector <- R6::R6Class( #' Write content to the connector.See also [write_cnt]. #' @return `r rd_connector_utils("inv_self")` write_cnt = function(x, name, ...) { - self %>% + self |> write_cnt(x, name, ...) }, @@ -90,7 +90,7 @@ connector <- R6::R6Class( #' Remove or delete content from the connector. See also [remove_cnt]. #' @return `r rd_connector_utils("inv_self")` remove_cnt = function(name, ...) { - self %>% + self |> remove_cnt(name, ...) } ) diff --git a/R/dbi.R b/R/dbi.R index a433f7c..04eda68 100644 --- a/R/dbi.R +++ b/R/dbi.R @@ -70,7 +70,7 @@ connector_dbi <- R6::R6Class( #' See also [disconnect_cnt]. #' @return [invisible] `self`. disconnect_cnt = function() { - self %>% + self |> disconnect_cnt() }, @@ -79,7 +79,7 @@ connector_dbi <- R6::R6Class( #' See also [tbl_cnt]. #' @return A [dplyr::tbl] object. tbl_cnt = function(name, ...) { - self %>% + self |> tbl_cnt(name, ...) } ), diff --git a/R/dbi_methods.R b/R/dbi_methods.R index a68649c..a60c464 100644 --- a/R/dbi_methods.R +++ b/R/dbi_methods.R @@ -18,7 +18,7 @@ #' @rdname read_cnt #' @export read_cnt.connector_dbi <- function(connector_object, name, ...) { - connector_object$conn %>% + connector_object$conn |> DBI::dbReadTable(name = name, ...) } @@ -41,7 +41,7 @@ read_cnt.connector_dbi <- function(connector_object, name, ...) { #' @rdname write_cnt #' @export write_cnt.connector_dbi <- function(connector_object, x, name, ...) { - connector_object$conn %>% + connector_object$conn |> DBI::dbWriteTable(name = name, value = x, ...) return(invisible(connector_object)) } @@ -59,7 +59,7 @@ write_cnt.connector_dbi <- function(connector_object, x, name, ...) { #' @rdname list_content_cnt #' @export list_content_cnt.connector_dbi <- function(connector_object, ...) { - connector_object$conn %>% + connector_object$conn |> DBI::dbListTables(...) } @@ -81,7 +81,7 @@ list_content_cnt.connector_dbi <- function(connector_object, ...) { #' @rdname remove_cnt #' @export remove_cnt.connector_dbi <- function(connector_object, name, ...) { - connector_object$conn %>% + connector_object$conn |> DBI::dbRemoveTable(name = name, ...) return(invisible(connector_object)) } @@ -113,7 +113,7 @@ remove_cnt.connector_dbi <- function(connector_object, name, ...) { #' @rdname tbl_cnt #' @export tbl_cnt.connector_dbi <- function(connector_object, name, ...) { - connector_object$conn %>% + connector_object$conn |> dplyr::tbl(from = name, ...) } @@ -133,6 +133,6 @@ tbl_cnt.connector_dbi <- function(connector_object, name, ...) { #' @rdname disconnect_cnt #' @export disconnect_cnt.connector_dbi <- function(connector_object, ...) { - connector_object$conn %>% + connector_object$conn |> DBI::dbDisconnect(...) } diff --git a/R/fs.R b/R/fs.R index e4144ba..b6a22e1 100644 --- a/R/fs.R +++ b/R/fs.R @@ -57,7 +57,7 @@ connector_fs <- R6::R6Class( #' See also [download_cnt]. #' @return [invisible] `file` download_cnt = function(name, file = basename(name), ...) { - self %>% + self |> download_cnt(name, file, ...) }, @@ -66,7 +66,7 @@ connector_fs <- R6::R6Class( #' See also [upload_cnt]. #' @return `r rd_connector_utils("inv_self")` upload_cnt = function(file, name = basename(file), ...) { - self %>% + self |> upload_cnt(file, name, ...) }, @@ -76,7 +76,7 @@ connector_fs <- R6::R6Class( #' @param name [character] The name of the directory to create #' @return [connector_fs] object of a newly created directory create_directory_cnt = function(name, ...) { - self %>% + self |> create_directory_cnt(name, ...) }, @@ -86,7 +86,7 @@ connector_fs <- R6::R6Class( #' @param name [character] The name of the directory to remove #' @return `r rd_connector_utils("inv_self")` remove_directory_cnt = function(name, ...) { - self %>% + self |> remove_directory_cnt(name, ...) } ), diff --git a/R/fs_methods.R b/R/fs_methods.R index 06238d6..4ee212a 100644 --- a/R/fs_methods.R +++ b/R/fs_methods.R @@ -71,7 +71,7 @@ write_cnt.connector_fs <- function(connector_object, x, name, ...) { #' @rdname list_content_cnt #' @export list_content_cnt.connector_fs <- function(connector_object, ...) { - connector_object$path %>% + connector_object$path |> list.files(...) } diff --git a/R/utils-pipe.R b/R/utils-pipe.R deleted file mode 100644 index fd0b1d1..0000000 --- a/R/utils-pipe.R +++ /dev/null @@ -1,14 +0,0 @@ -#' Pipe operator -#' -#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. -#' -#' @name %>% -#' @rdname pipe -#' @keywords internal -#' @export -#' @importFrom magrittr %>% -#' @usage lhs \%>\% rhs -#' @param lhs A value or the magrittr placeholder. -#' @param rhs A function call using the magrittr semantics. -#' @return The result of calling `rhs(lhs)`. -NULL diff --git a/R/utils_files.R b/R/utils_files.R index 0177a04..c71ef74 100644 --- a/R/utils_files.R +++ b/R/utils_files.R @@ -58,14 +58,14 @@ assert_ext <- function(ext, method) { #' @importFrom rlang set_names #' @noRd error_extension <- function() { - ext_supp <- supported_fs() %>% + ext_supp <- supported_fs() |> rlang::set_names("*") c( "No method found for this extension, please implement your own method (to see an example run `connector::example_read_ext()`) or use a supported extension", "i" = "Supported extensions are:", ext_supp - ) %>% + ) |> cli::cli_abort() } diff --git a/dev/connector_config_yaml.qmd b/dev/connector_config_yaml.qmd index 2430961..3f5b468 100644 --- a/dev/connector_config_yaml.qmd +++ b/dev/connector_config_yaml.qmd @@ -55,7 +55,7 @@ extract_datasources(yaml_content) only_one <- extract_connections(yaml_content)[[1]] ## Extract fct -my_backend <- only_one %>% +my_backend <- only_one |> extract_backends() #### Create the backend for fs @@ -124,8 +124,8 @@ connect$sdtm$read("iris") ## Manipulate a table with the database -connect$sdtm$tbl("iris") %>% +connect$sdtm$tbl("iris") |> dplyr::filter(Sepal.Length > 5) -``` \ No newline at end of file +``` diff --git a/man/pipe.Rd b/man/pipe.Rd deleted file mode 100644 index a648c29..0000000 --- a/man/pipe.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils-pipe.R -\name{\%>\%} -\alias{\%>\%} -\title{Pipe operator} -\usage{ -lhs \%>\% rhs -} -\arguments{ -\item{lhs}{A value or the magrittr placeholder.} - -\item{rhs}{A function call using the magrittr semantics.} -} -\value{ -The result of calling \code{rhs(lhs)}. -} -\description{ -See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. -} -\keyword{internal} diff --git a/tests/testthat/_snaps/connect.md b/tests/testthat/_snaps/connect.md index 81033c4..6ee643b 100644 --- a/tests/testthat/_snaps/connect.md +++ b/tests/testthat/_snaps/connect.md @@ -1,7 +1,7 @@ # Connect datasources to the connections for a yaml file Code - iris_f %>% dplyr::collect() + dplyr::collect(iris_f) Output # A tibble: 118 x 5 Sepal.Length Sepal.Width Petal.Length Petal.Width Species diff --git a/tests/testthat/test-connect.R b/tests/testthat/test-connect.R index 6689b4b..936ddb0 100644 --- a/tests/testthat/test-connect.R +++ b/tests/testthat/test-connect.R @@ -7,11 +7,11 @@ test_that("Connect datasources to the connections for a yaml file", { ## write and read for a system file withr::with_options(list(readr.show_col_types = FALSE), { - cnts$adam$read_cnt("adsl.csv") %>% + cnts$adam$read_cnt("adsl.csv") |> expect_s3_class("data.frame") expect_error(cnts$adam$read_cnt("do_not_exits.csv")) - cnts$adam$write_cnt(data.frame(a = 1:10, b = 11:20), "example.csv") %>% + cnts$adam$write_cnt(data.frame(a = 1:10, b = 11:20), "example.csv") |> expect_no_error() expect_no_error(cnts$adam$read_cnt("example.csv")) @@ -26,12 +26,12 @@ test_that("Connect datasources to the connections for a yaml file", { ## Manipulate a table with the database - iris_f <- cnts$sdtm$tbl_cnt("iris") %>% + iris_f <- cnts$sdtm$tbl_cnt("iris") |> dplyr::filter(Sepal.Length > 5) expect_s3_class(iris_f, "tbl_dbi") - expect_snapshot(iris_f %>% dplyr::collect()) + expect_snapshot(dplyr::collect(iris_f)) }) test_that("Tools for yaml parsinbg", { @@ -124,9 +124,13 @@ testthat::test_that("Using and uptade metadata", { }) test_that("Add logs to connectors object",{ - # Don't test the logic of connector.logger because it is not the purpose of connector - cnts <- connect(yaml_file, logging = TRUE) + # connector.logger needs to be installed to pass this test - if not available + # then skip the test + testthat::skip_if_not_installed("connector.logger") + + # Don't test the logic of connector.logger because it is not the purpose of connector + cnts <- connect(yaml_file, logging = TRUE) lapply(cnts, function(x){ expect_s3_class(x, "connector") diff --git a/vignettes/connector.Rmd b/vignettes/connector.Rmd index 2a62519..08f1abb 100644 --- a/vignettes/connector.Rmd +++ b/vignettes/connector.Rmd @@ -146,25 +146,25 @@ library(dplyr) # Manipulate data ## Iris data -setosa <- iris %>% +setosa <- iris |> filter(Species == "setosa") -mean_for_all_iris <- iris %>% - group_by(Species) %>% +mean_for_all_iris <- iris |> + group_by(Species) |> summarise_all(list(mean, median, sd, min, max)) ## Mtcars data -cars <- mtcars %>% +cars <- mtcars |> filter(mpg > 22) -mean_for_all_mtcars <- mtcars %>% - group_by(gear) %>% +mean_for_all_mtcars <- mtcars |> + group_by(gear) |> summarise( across( everything(), list("mean" = mean, "median" = median, "sd" = sd, "min" = min, "max" = max), .names = "{.col}_{.fn}") - ) %>% + ) |> tidyr::pivot_longer( cols = -gear, names_to = c(".value", "stat"), @@ -172,16 +172,16 @@ mean_for_all_mtcars <- mtcars %>% ) ## Store data -db$adam %>% +db$adam |> write_cnt(setosa, "setosa", overwrite = TRUE) -db$adam %>% +db$adam |> write_cnt(mean_for_all_iris, "mean_iris", overwrite = TRUE) -db$adam %>% +db$adam |> write_cnt(cars, "cars_mpg", overwrite = TRUE) -db$adam %>% +db$adam |> write_cnt(mean_for_all_mtcars, "mean_mtcars", overwrite = TRUE) ``` @@ -195,13 +195,13 @@ library(tidyr) library(ggplot2) # List and load data -db$adam %>% +db$adam |> list_content_cnt() -table <- db$adam %>% +table <- db$adam |> read_cnt("mean_mtcars") -gttable <- table %>% +gttable <- table |> gt(groupname_col = "gear") ## Save table @@ -214,8 +214,8 @@ gtsave(gttable, tmp_file) db$output_sh$upload_cnt(tmp_file, "tmeanallmtcars.docx") # Manipulate data -setosa_fsetosa <- db$adam %>% - read_cnt("setosa") %>% +setosa_fsetosa <- db$adam |> + read_cnt("setosa") |> filter(Sepal.Length > 5) fsetosa <- ggplot(setosa) + @@ -277,27 +277,27 @@ config_file <- system.file("config", "config_file_system.yml", package = "connec db <- connect(config = config_file) ## Iris data -setosa <- iris %>% +setosa <- iris |> filter(Species == "setosa") -mean_for_all_iris <- iris %>% - group_by(Species) %>% +mean_for_all_iris <- iris |> + group_by(Species) |> summarise_all(list(mean, median, sd, min, max)) ## Store data -db$adam %>% +db$adam |> write_cnt(setosa, "setosa.rds") -db$adam %>% +db$adam |> write_cnt(mean_for_all_iris, "mean_iris.rds") ## List and load data -db$adam %>% +db$adam |> list_content_cnt() # Manipulate data -setosa_fsetosa <- db$adam %>% - read_cnt("setosa") %>% +setosa_fsetosa <- db$adam |> + read_cnt("setosa") |> filter(Sepal.Length > 5) fsetosa <- ggplot(setosa) + From 39ef67afea62dae3e3310b534820c1b714411482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KTQN=20=28Kristian=20Tr=C3=B8jelsgaard=20Nielsen=29?= Date: Mon, 2 Dec 2024 14:05:22 +0100 Subject: [PATCH 2/3] Substitute tools::file_ext() with get_file_ext() --- R/connect.R | 2 +- R/fs_read.R | 4 ++-- R/fs_write.R | 2 +- R/utils_fileext.R | 14 ++++++++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 R/utils_fileext.R diff --git a/R/connect.R b/R/connect.R index 2fcf448..c7f5362 100644 --- a/R/connect.R +++ b/R/connect.R @@ -82,7 +82,7 @@ connect <- function(config = "_connector.yml", metadata = NULL, datasource = NUL checkmate::assert_logical(logging) if (!is.list(config)) { - if (tools::file_ext(config) %in% c("yml", "yaml")) { + if (get_file_ext(config) %in% c("yml", "yaml")) { config <- read_file(config, eval.expr = TRUE) } else { config <- read_file(config) diff --git a/R/fs_read.R b/R/fs_read.R index 5c2dbaf..eb1b5cd 100644 --- a/R/fs_read.R +++ b/R/fs_read.R @@ -10,7 +10,7 @@ #' @return the result of the reading function #' @export read_file <- function(path, ...) { - find_ext <- tools::file_ext(path) + find_ext <- get_file_ext(path) class(path) <- c(find_ext, class(path)) @@ -127,4 +127,4 @@ read_ext.yaml <- read_ext.yml #' @export read_ext.json <- function(path, ...) { jsonlite::read_json(path = path, ...) -} \ No newline at end of file +} diff --git a/R/fs_write.R b/R/fs_write.R index 0004c98..59ba09a 100644 --- a/R/fs_write.R +++ b/R/fs_write.R @@ -11,7 +11,7 @@ #' @return `write_file()`: [invisible()] file. #' @export write_file <- function(x, file, ...) { - find_ext <- tools::file_ext(file) |> + find_ext <- get_file_ext(file) |> assert_ext("write_ext") class(file) <- c(find_ext, class(file)) diff --git a/R/utils_fileext.R b/R/utils_fileext.R new file mode 100644 index 0000000..4dbbe6e --- /dev/null +++ b/R/utils_fileext.R @@ -0,0 +1,14 @@ +# Getting file extension +get_file_ext <- function(file_paths) { + vapply( + X = file_paths, + FUN = function(file_path) { + file_name <- basename(file_path) + file_parts <- strsplit(file_name, "\\.")[[1]] + file_extension <- ifelse(length(file_parts) == 1, "", utils::tail(file_parts, 1)) + return(file_extension) + }, + FUN.VALUE = character(1), + USE.NAMES = FALSE + ) +} From d54d43498de789be03f5d2953563fa6e8f0890e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KTQN=20=28Kristian=20Tr=C3=B8jelsgaard=20Nielsen=29?= Date: Tue, 3 Dec 2024 11:01:02 +0100 Subject: [PATCH 3/3] Updating NEWS and DESCRIPTION --- DESCRIPTION | 4 ++-- NEWS.md | 30 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c8eca26..e265981 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: connector Title: Connect to your data easily -Version: 0.0.3 +Version: 0.0.4 Authors@R: c( person("Cervan", "Girard", , "cgid@novonordisk.com", role = c("aut", "cre")), person("Aksel", "Thomsen", , "oath@novonordisk.com", role = "aut"), @@ -46,6 +46,6 @@ Config/testthat/edition: 3 Encoding: UTF-8 Roxygen: list(markdown = TRUE, r6 = TRUE) RoxygenNote: 7.3.2 -Remotes: +Remotes: NovoNordisk-OpenSource/zephyr, NovoNordisk-OpenSource/connector.logger diff --git a/NEWS.md b/NEWS.md index 5b29b28..b6436c7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,33 @@ +# connector 0.0.4 (2024-12-03) + +### Migration: +- Migration to public github + +### Features: +- Update of `create_directory_cnt()` +- Added metadata as a parameter in `connect()` +- More comprehensive testing +- Better integration with [whirl](https://github.com/NovoNordisk-OpenSource/whirl) through [connector.logger](https://github.com/NovoNordisk-OpenSource/connector.logger) + +### Other: +- Reducing the number of dependencies. +- Better messages + +# connector 0.0.3 (2024-09-25) + +### Breaking Changes: +- rename function from `*_cnt` to `cnt_` + +### Features: +- Nested connectors objects +- Use active bindings +- User guide added + + # connector 0.0.2 -* Added `connectors` super class +- Added `connectors` super class # connector 0.0.1 -* Initial version +- Initial version