-
Notifications
You must be signed in to change notification settings - Fork 28
V7.0 dev feat colnames #451
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
Open
timcadman
wants to merge
6
commits into
v7.0-dev
Choose a base branch
from
v7.0-dev-feat-colnames
base: v7.0-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e3e98bc
refactor colnames
timcadman c130e19
added reusable functions
timcadman 490c1f8
added tests
timcadman 4305357
redocumented package
timcadman 57de79f
added PR template
timcadman 40eb96a
use get instead of eval as more secure
timcadman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| ## Instructions & checklist for PR author | ||
|
|
||
| ### Description of changes | ||
| [Add descriptions of changes made] | ||
|
|
||
| ### Refactor instructions | ||
| - [ ] Replaced `x <- eval(parse(text = x.name), envir = parent.frame())` with `x <- .loadServersideObject(x)` | ||
| - [ ] If necessary, check the class of the object using `.checkClass()` | ||
|
|
||
| ### Testing instructions | ||
| - [ ] Writen server-side unit tests for unhappy flow | ||
| - [ ] Run `devtools::test(filter = "smk-|disc|arg")` and check it passes | ||
| - [ ] Run `devtools::check(args = '--no-tests')` and check it passes (we run tests separately to skip performance checks) | ||
| - [ ] Run `devtools::build()` and check it builds without errors | ||
|
|
||
| ## Instructions & checklist for PR reviewers | ||
| - [ ] Run `devtools::test(filter = "smk-|disc|arg")` and check it passes | ||
| - [ ] Run `devtools::check(args = '--no-tests')` and check it passes (we run tests separately to skip performance checks) | ||
| - [ ] Run `devtools::build()` and check it builds without errors | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #' | ||
| #' @title Returns the column names of a data frame or matrix | ||
| #' @description This function is similar to R function \code{colnames}. | ||
| #' @details The function returns the column names of the input dataframe or matrix | ||
| #' @param x a string character, the name of a dataframe or matrix | ||
| #' @return the column names of the input object | ||
| #' @author Demetris Avraam, for DataSHIELD Development Team | ||
| #' @export | ||
| #' | ||
| colnamesDS <- function(x){ | ||
| x.val <- .loadServersideObject(x) | ||
| .checkClass(obj = x.val, obj_name = x, permitted_classes = c("data.frame", "matrix")) | ||
| out <- colnames(x.val) | ||
| return(out) | ||
| } | ||
| #AGGREGATE FUNCTION | ||
| # colnamesDS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #' Load a Server-Side Object by Name | ||
| #' | ||
| #' Evaluates a character string referring to an object name and returns the corresponding | ||
| #' object from the parent environment. If the object does not exist, an error is raised. | ||
| #' | ||
| #' @param x A character string naming the object to be retrieved. | ||
| #' @return The evaluated R object referred to by `x`. | ||
| #' @noRd | ||
| .loadServersideObject <- function(x) { | ||
| tryCatch( | ||
| get(x, envir = parent.frame(2)), | ||
| error = function(e) { | ||
| stop("The server-side object", " '", x, "' ", "does not exist") | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| #' Check Class of a Server-Side Object | ||
| #' | ||
| #' Verifies that a given object is of an allowed class. If not, raises an informative error | ||
| #' message listing the permitted classes and the actual class of the object. | ||
| #' | ||
| #' @param obj The object whose class should be checked. | ||
| #' @param obj_name A character string with the name of the object (used in error messages). | ||
| #' @param permitted_classes A character vector of allowed class names. | ||
| #' @importFrom glue glue glue_collapse | ||
| #' @return Invisibly returns `TRUE` if the class check passes; otherwise throws an error. | ||
| #' @noRd | ||
| .checkClass <- function(obj, obj_name, permitted_classes) { | ||
| typ <- class(obj) | ||
|
|
||
| if (!any(permitted_classes %in% typ)) { | ||
| msg <- glue( | ||
| "The server-side object must be of type {glue_collapse(permitted_classes, sep = ' or ')}. ", | ||
| "'{obj_name}' is type {typ}." | ||
| ) | ||
|
|
||
| stop(msg, call. = FALSE) | ||
| } | ||
|
|
||
| invisible(TRUE) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #------------------------------------------------------------------------------- | ||
| # Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. | ||
| # Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved. | ||
| # | ||
| # This program and the accompanying materials | ||
| # are made available under the terms of the GNU Public License v3.0. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| #------------------------------------------------------------------------------- | ||
|
|
||
| # | ||
| # Set up | ||
| # | ||
|
|
||
| # context("colnamesDS::smk::setup") | ||
|
|
||
| # | ||
| # Tests | ||
| # | ||
|
|
||
| # context("colnamesDS::smk::data.frame") | ||
| test_that("simple colnamesDS, data.frame", { | ||
| input <- data.frame(v1 = c(0.0, 1.0, 2.0, 3.0, 4.0), v2 = c(4.0, 3.0, 2.0, 1.0, 0.0)) | ||
|
|
||
| res <- colnamesDS("input") | ||
|
|
||
| expect_equal(class(res), "character") | ||
| expect_length(res, 2) | ||
| expect_true("v1" %in% res) | ||
| expect_true("v2" %in% res) | ||
| }) | ||
|
|
||
| # context("colnamesDS::smk::data.matrix") | ||
| test_that("simple colnamesDS, data.matrix", { | ||
| input <- data.matrix(data.frame(v1 = c(0.0, 1.0, 2.0, 3.0, 4.0), v2 = c(4.0, 3.0, 2.0, 1.0, 0.0))) | ||
|
|
||
| res <- colnamesDS("input") | ||
|
|
||
| expect_equal(class(res), "character") | ||
| expect_length(res, 2) | ||
| expect_true("v1" %in% res) | ||
| expect_true("v2" %in% res) | ||
| }) | ||
|
|
||
| test_that("colnamesDS throws error when object does not exist", { | ||
| expect_error( | ||
| colnamesDS("nonexistent_object"), | ||
| regexp = "does not exist" | ||
| ) | ||
| }) | ||
|
|
||
| test_that("colnamesDS throws error when object is not data.frame or matrix", { | ||
| bad_input <- list(a = 1:3, b = 4:6) | ||
| expect_error( | ||
| colnamesDS("bad_input"), | ||
| regexp = "must be of type data.frame or matrix" | ||
| ) | ||
| }) | ||
|
|
||
| # | ||
| # Done | ||
| # | ||
|
|
||
| # context("colnamesDS::smk::shutdown") | ||
|
|
||
| # context("colnamesDS::smk::done") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
|
|
||
| #------------------------------------------------------------------------------- | ||
| # Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. | ||
| # | ||
| # This program and the accompanying materials | ||
| # are made available under the terms of the GNU Public License v3.0. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| #------------------------------------------------------------------------------- | ||
|
|
||
| # | ||
| # Set up | ||
| # | ||
|
|
||
| ## When .loadServersideObject is called, the actual data exists two levels below the function, | ||
| ## i.e. data in global env --> ds function --> .loadServersideObject. We recreate this in | ||
| ## the test environment with a wrapper function. | ||
| .dsFunctionWrapper <- function(x) { | ||
| .loadServersideObject(x) | ||
| } | ||
|
|
||
| # context("utils::smk::setup") | ||
| test_that(".loadServersideObject() returns existing object", { | ||
| test_df <- data.frame(a = 1:3) | ||
| result <- .dsFunctionWrapper("test_df") | ||
| expect_identical(result, test_df) | ||
| }) | ||
|
|
||
| test_that(".loadServersideObject() throws error for missing object", { | ||
| expect_error( | ||
| .dsFunctionWrapper("test_df"), | ||
| regexp = "does not exist" | ||
| ) | ||
| }) | ||
|
|
||
| test_that(".checkClass() passes for correct class", { | ||
| df <- data.frame(a = 1) | ||
| expect_invisible( | ||
| .checkClass(df, "df", c("data.frame", "matrix")) | ||
| ) | ||
| }) | ||
|
|
||
| test_that(".checkClass() throws informative error for wrong class", { | ||
| x <- list(a = 1) | ||
| expect_error( | ||
| .checkClass(x, "x", c("data.frame", "matrix")), | ||
| regexp = "must be of type data.frame or matrix" | ||
| ) | ||
| }) | ||
|
|
||
| # context("utils::smk::shutdown") | ||
| # context("utils::smk::done") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes objects have multiple classes, which might create multiple error messages.
So I think it would be a good idea to also use
glue_collapseto printtyp.