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

Improved compatibility with list casting operations and added unclassing to as.list #15

Merged
merged 4 commits into from
Oct 18, 2024
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: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v0.4.2

### Improvements

- Improved compatibility of `dsoParams` with list operations

## v0.4.1

### Improvements

- Improve the readability of `dsoParams` object using `read_params()`
- Improve the readability of `dsoParams` object using `read_params()`

## v0.4.0

Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: dso
Type: Package
Title: dso R companion package
Version: 0.4.0
Version: 0.4.2
Author: Daniel Schreyer<[email protected]>,
Gregor Sturm<[email protected]>,
Thomas Schwarzl<[email protected]>
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export(read_params)
export(safe_get)
export(set_stage)
export(stage_here)
exportMethods(as.list)
exportMethods(show)
importFrom(glue,glue)
importFrom(here,here)
Expand Down
20 changes: 19 additions & 1 deletion R/class-dsoParams.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dsoParams <- function( x = list()) {
y
}
})
class(x) <- "dsoParams"
class(x) <- c("dsoParams", "list")
return(x)
}

Expand Down Expand Up @@ -68,3 +68,21 @@ setMethod(f = "show",
definition = function(object) {
cat(yaml::as.yaml(object))
})

# Custom as.list method for dsoParams class
#' @export
setMethod(
f = "as.list",
signature = "dsoParams",
definition = function(x) {
lapply(x, function(y) {
if("dsoParams" %in% class(y)) {
as.list(unclass(y))
} else {
x
}
})
}
)


36 changes: 33 additions & 3 deletions tests/testthat/test-class-dsoParams.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ test_that("dsoParams() creation of dsoParams objects", {
y$a <- list()
y$b <- "b"
y$a$c <- "c"
y <- dsoParams(y)
z <- dsoParams(y)

expect_s3_class(z, "dsoParams")
expect_s3_class(z$a, "dsoParams")

expect_s3_class(y, "dsoParams")
expect_s3_class(y$a, "dsoParams")
})


Expand Down Expand Up @@ -74,3 +75,32 @@ test_that("access to dsoParams() with $ and [[", {
expect_error(params[[var_z]][[var_c]], "does not exist")
})

test_that("dsoParams: as.list() convertion", {
y <- list()
y$a <- list()
y$b <- "b"
y$a$c <- "c"
y$a$d <- "d"
y$a$e <- list()
y$a$e$f <- "f"
z <- dsoParams(y)

l_z <- as.list(z)
expect_type(l_z, "list")
expect_equal(class(l_z), "list")
})

test_that("dsoParams: as.data.frame() convertion", {
y <- list()
y$a <- list()
y$b <- "b"
y$a$c <- "c"
y$a$d <- "d"
y$a$e <- list()
y$a$e$f <- "f"
z <- dsoParams(y)

l_z <- as.data.frame(z)
expect_s3_class(l_z, "data.frame")
})