# ---------------------------------------------------------------------------- #
#' Get field values in an RC object (with optional replacement)
#'
#' Retrieves a list of field values from an RC object, replacing any existing
#' field values with those supplied.
#'
#' @param rc_obj An RC object.
#' @param vals A list of fields to replace in \code{rc_obj}.
#'
#' @return A list of existing fields in \code{rc_obj} with values replaced by
#' any field values supplied in \code{vals}.
#'
#' @examples
#' thing_factory <- setRefClass(
#' "thing",
#' fields = list(name = "character", value = "numeric")
#' )
#' foofo <- thing_factory$new(name = "Flibberty Gibbet", value = 1e6)
#' get_rc_fld_vals(foofo)
#' get_rc_fld_vals(foofo, list(value = 25.6))
#'
#' @export
#'
get_rc_fld_vals <- function(rc_obj, vals = NULL) {
# Adapted from
# http://stackoverflow.com/questions/18713847/
# return-a-list-of-fields-of-a-reference-class
# [viewed 09jan20]
# Retrieve list of fields and set all elements to NULL
flds <- rc_obj$getRefClass()$fields()
flds <- lapply(flds, function(x) NULL)
# Set field values to those in rc_obj
for (nm in names(flds)) flds[[nm]] <- rc_obj$field(nm)
# Replace any values with those supplied in vals
if (!is.null(vals)) flds[names(flds) %in% names(vals)] <- vals
flds
}
# ---------------------------------------------------------------------------- #
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.