Skip to content
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
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(changeRefGroupDS)
export(checkNegValueDS)
export(checkPermissivePrivacyControlLevel)
export(classDS)
export(colnamesDS)
export(completeCasesDS)
export(corDS)
export(corTestDS)
Expand Down Expand Up @@ -139,3 +140,5 @@ import(gamlss.dist)
import(mice)
importFrom(gamlss.dist,pST3)
importFrom(gamlss.dist,qST3)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
20 changes: 20 additions & 0 deletions PULL_REQUEST_TEMPLATE.md
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

17 changes: 17 additions & 0 deletions R/colnamesDS.R
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
42 changes: 42 additions & 0 deletions R/utils.R
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}."
Copy link
Contributor

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.

> a <- tibble::tibble("1")
> class(a)
[1] "tbl_df"     "tbl"        "data.frame"
> typ <- class(a)
> glue::glue("hello {typ}")
hello tbl_df
hello tbl
hello data.frame

So I think it would be a good idea to also use glue_collapse to print typ.

)

stop(msg, call. = FALSE)
}

invisible(TRUE)
}
23 changes: 23 additions & 0 deletions man/colnamesDS.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions tests/testthat/test-smk-colnamesDS.R
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")
53 changes: 53 additions & 0 deletions tests/testthat/test-smk-utils.R
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")
Loading