Newer
Older
#============================== GENERAL UTILITY (copy playground) ================================
###Stub
# Stub <- R6::R6Class("Stub",
# public = list(
#
# initialize = function(argument) {
# #stopifnot(is.character(name), length(name) == 1)
# #...
# }
# ),
#
# active = list(
#
# ),
#
# private = list(
#
# )
# )
Ruben Heinrich
committed
#============================== VALIDATION UTILITY ================================
#'check_for_obj_of_name
#'@description Checks if a object with the given name was already defined for a ogs6 object and if not,
#' tells user to initialize one
#'@param ogs6_obj The ogs6 object to check
#'@param obj_name The name of the object to check for
check_for_obj_of_name <- function(ogs6_obj, obj_name) {
if(!obj_name %in% names(ogs6_obj$sim_input)){
stop(paste("There is no object named ", obj_name," for your ogs6 object yet.\n
You can initialize one by calling input_add_", obj_name ,
"() (read up on the required parameters)"),
call. = FALSE)
}
}
#============================== XML UTILITY ================================
#' export_xml_to_file
#' @description Export function
#' @param xml_data The data to be exported (already in XML friendly format)
#' @param file_name The name of the file to be written
# @examples
Ruben Heinrich
committed
# export_xml_to_file(...)
export_xml_to_file <- function(xml_data, file_name) {
doc <- xml2::as_xml_document(xml_data)
xml2::write_xml(doc, file_name, options = "format", encoding="ISO-8859-1")
invisible()
}
Ruben Heinrich
committed
#' adopt_nodes
#' @description A helper function for creating parent nodes using the generic function as_node
#' @param parent_name The name of the new parent node
#' @param objs A list of class objects (class must have method for generic function as_node)
adopt_nodes <- function(parent_name, objs) {
parent_node <- list(parent_name = list())
Ruben Heinrich
committed
for(i in 1:length(objs)) {
parent_node <- c(parent_node[[1]], as_node(objs[[i]]))
}
Ruben Heinrich
committed
return(xml2::as_xml_document(parent_node))
}
#'add_opt_attr
#'@description Adds an optional attribute to a node attribute list
#'@param node The node the optional attribute should be added to
#'@param obj_parameter The value of the attribute to be added
#'@param attr_name The name of the attribute to be added
add_opt_attr <- function(node, obj_parameter, attr_name) {
if(!is.null(obj_parameter)) {
attributes(node[[1]])[[attr_name]] <- obj_parameter
Ruben Heinrich
committed
return(node)
Ruben Heinrich
committed
#'add_opt_child
#'@description Adds an optional child to a node child list
#'@param node The node the optional child should be added to
#'@param obj_parameter The value of the child to be added
#'@param child_name Optional: If it's a child node instead of just a value, the name of the child to be added
add_opt_child <- function(node, obj_parameter, child_name = NULL) {
if(!is.null(obj_parameter)) {
if(!is.null(child_name) && length(node[[1]]) == length(names(node[[1]]))) {
node[[1]] <- c(node[[1]], list(child_name = obj_parameter))
}else if(length(node[[1]]) == 0) {
node[[1]] <- list(obj_parameter)
}else{
stop(paste("You're trying to add a value (an unnamed child node) to a node
which already has a value."), call. = FALSE)
}
Ruben Heinrich
committed
}
Ruben Heinrich
committed
return(node)
}
Ruben Heinrich
committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#================================Test if S3 object in R6 class inherits reference semantics
# A <- R6::R6Class("A",
# public = list(
#
# b_obj = NULL,
#
# initialize = function(b_obj) {
# self$b_obj <- b_obj
# }
# )
# )
#
# b <- function(x){
# structure(x,
# class = "b")
# }
#
# mod_func_a <- function(a_obj){
# a_obj$b_obj$x <- 100
# }
#
# a_obj <- A$new(b(42))
#
# mod_func_a(a_obj)
#
# a_obj$b_obj$x
#
#
# mod_func_b <- function(b_obj){
# b_obj$x <- 100
# }
#
# b_obj <- b(42)
#
# mod_func_b(b_obj)
#
# b_obj