Skip to content

Commit

Permalink
apply styler (#13)
Browse files Browse the repository at this point in the history
* apply styler

* fix working directory when linting in ci

* fix linting issues
  • Loading branch information
rcannood authored Aug 17, 2024
1 parent 64e9c16 commit 1e34e8f
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 38 deletions.
1 change: 1 addition & 0 deletions .github/workflows/R-packages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
with:
extra-packages: any::lintr, local::.
needs: lint
working-directory: packages/r/${{ matrix.package }}

- name: Lint -- run `styler::style_pkg()` if this fails!
working-directory: packages/r/${{ matrix.package }}
Expand Down
3 changes: 2 additions & 1 deletion packages/r/openproblems/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
^LICENSE\.md$
^\.gitignore$
^CHANGELOG\.md$
^CHANGELOG\.md$
^\.lintr$
3 changes: 3 additions & 0 deletions packages/r/openproblems/.lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters: linters_with_defaults(
line_length_linter(120)
)
2 changes: 2 additions & 0 deletions packages/r/openproblems/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Initial release

* `find_project_root`: simplify implementation (PR #11).

* Clean up code formatting with `styler::style_pkg()` (PR #13).

## TESTING

* Add tests for `find_project_root` (PR #11).
8 changes: 4 additions & 4 deletions packages/r/openproblems/R/deep_merge.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#' Deep merge two lists
#'
#'
#' This function will merge two lists recursively. If both lists
#' are named lists, the keys will be merged. If both lists are
#' lists, they will be appended. Otherwise, the second list will
#' override the first list.
#'
#'
#' @param obj1 The first object
#' @param obj2 The second object
#' @return The merged list
#'
#'
#' @noRd
#' @examples
#' deep_merge(list(a = 1, b = 2), list(b = 3, c = 4))
Expand Down Expand Up @@ -44,4 +44,4 @@ deep_merge <- function(obj1, obj2) {
# else override list1 with list2
obj2
}
}
}
2 changes: 1 addition & 1 deletion packages/r/openproblems/R/find_project_root.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ find_project_root <- function(path) {
}

as.character(path)
}
}
20 changes: 10 additions & 10 deletions packages/r/openproblems/R/is_named_list.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#' Check if an object is a named list
#'
#'
#' @param obj An object
#' @return TRUE if the object is a named list, FALSE otherwise
#'
#'
#' @noRd
#'
#' @examples
#' is_named_list(list(a = 1, b = 2)) # TRUE
#' is_named_list(list(1, 2)) # FALSE
#' is_named_list(NULL) # TRUE
#' is_named_list(list()) # TRUE
#' is_named_list(c(1, 2, 3)) # FALSE
#'
#' @examples
#' is_named_list(list(a = 1, b = 2)) # TRUE
#' is_named_list(list(1, 2)) # FALSE
#' is_named_list(NULL) # TRUE
#' is_named_list(list()) # TRUE
#' is_named_list(c(1, 2, 3)) # FALSE
is_named_list <- function(obj) {
is.null(obj) || (is.list(obj) && (length(obj) == 0 || !is.null(names(obj))))
}
}
34 changes: 20 additions & 14 deletions packages/r/openproblems/R/read_nested_yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@
#' }
read_nested_yaml <- function(path, project_path = find_project_root(path)) {
path <- normalizePath(path, mustWork = FALSE)
data <- tryCatch({
suppressWarnings(yaml::read_yaml(path))
}, error = function(e) {
stop("Could not read ", path, ". Error: ", e)
})
data <- tryCatch(
{
suppressWarnings(yaml::read_yaml(path))
},
error = function(e) {
stop("Could not read ", path, ". Error: ", e)
}
)
process_nested_yaml(data, data, path, project_path)
}

#' Process the merge keys in a YAML
#'
#'
#' This function will recursively process the merge keys in a YAML
#'
#'
#' @param data The YAML data
#' @param root_data The root YAML data
#' @param path The path to the current YAML file
#' @param project_path The path to the root of the Viash project
#'
#'
#' @noRd
process_nested_yaml <- function(data, root_data, path, project_path) {
process_nested_yaml <- function(data, root_data, path, project_path) { # nolint cyclocomp_linter
if (is_named_list(data)) {
# check whether children have `__merge__` entries
processed_data <- lapply(data, function(dat) {
Expand Down Expand Up @@ -72,11 +75,14 @@ process_nested_yaml <- function(data, root_data, path, project_path) {
new_data_path <- normalizePath(new_data_path, mustWork = FALSE)

# read in the new data
tryCatch({
suppressWarnings(yaml::read_yaml(new_data_path))
}, error = function(e) {
stop("Could not read ", new_data_path, ". Error: ", e)
})
tryCatch(
{
suppressWarnings(yaml::read_yaml(new_data_path))
},
error = function(e) {
stop("Could not read ", new_data_path, ". Error: ", e)
}
)
}
x_root <- x

Expand Down
5 changes: 2 additions & 3 deletions packages/r/openproblems/R/read_viash_config.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
#'
#' @export
read_viash_config <- function(
target_config_path,
project_root_dir = find_project_root(target_config_path)
) {
target_config_path,
project_root_dir = find_project_root(target_config_path)) {
# note: if this config was not generated by viash, use `viash config view` first?
config <- read_nested_yaml(target_config_path)

Expand Down
2 changes: 1 addition & 1 deletion packages/r/openproblems/R/resolve_path.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ resolve_path <- function(path, project_path, parent_path) {
paste0(project_path, path),
fs::path_abs(path, parent_path)
)
}
}
3 changes: 1 addition & 2 deletions packages/r/openproblems/R/strip_margin.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Strip margin from a string
#'
#' @param text A character vector.
Expand All @@ -13,4 +12,4 @@
#' |")
strip_margin <- function(text, symbol = "\\|") {
gsub(paste0("(^|\n)[ \t]*", symbol), "\\1", text)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

test_that("find_project_root works", {
# create a temporary directory with a _viash.yaml file
# project/
Expand Down Expand Up @@ -54,4 +53,4 @@ test_that("find_project_root works", {
expect_null(
find_project_root(temp_dir)
)
})
})

0 comments on commit 1e34e8f

Please sign in to comment.