Skip to content

Commit c130e19

Browse files
committed
added reusable functions
1 parent e3e98bc commit c130e19

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

R/utils.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#' Load a Server-Side Object by Name
2+
#'
3+
#' Evaluates a character string referring to an object name and returns the corresponding
4+
#' object from the parent environment. If the object does not exist, an error is raised.
5+
#'
6+
#' @param x A character string naming the object to be retrieved.
7+
#' @return The evaluated R object referred to by `x`.
8+
#' @noRd
9+
.loadServersideObject <- function(x) {
10+
tryCatch(
11+
eval(parse(text = x), envir = parent.frame(2)),
12+
error = function(e) {
13+
stop("The server-side object", " '", x, "' ", "does not exist")
14+
}
15+
)
16+
}
17+
18+
#' Check Class of a Server-Side Object
19+
#'
20+
#' Verifies that a given object is of an allowed class. If not, raises an informative error
21+
#' message listing the permitted classes and the actual class of the object.
22+
#'
23+
#' @param obj The object whose class should be checked.
24+
#' @param obj_name A character string with the name of the object (used in error messages).
25+
#' @param permitted_classes A character vector of allowed class names.
26+
#' @importFrom glue glue glue_collapse
27+
#' @return Invisibly returns `TRUE` if the class check passes; otherwise throws an error.
28+
#' @noRd
29+
.checkClass <- function(obj, obj_name, permitted_classes) {
30+
typ <- class(obj)
31+
32+
if (!any(permitted_classes %in% typ)) {
33+
msg <- glue(
34+
"The server-side object must be of type {glue_collapse(permitted_classes, sep = ' or ')}. ",
35+
"'{obj_name}' is type {typ}."
36+
)
37+
38+
stop(msg, call. = FALSE)
39+
}
40+
41+
invisible(TRUE)
42+
}

0 commit comments

Comments
 (0)