Skip to content
Prev 247391 / 398506 Next

Help on a Display function

William Dunlap <wdunlap <at> tibco.com> writes:
I appreciate this extensive answer.
So I decided to use the following function, suppressing the deparsed form
if there is a tag (while I'm inclined to forbid function code as input):

    display <- function (...) {
        evaluatedArgs <- list(...)
        n <- length(evaluatedArgs)
        argTags <- names(evaluatedArgs)
        deparsedArgs <- lapply(substitute(placeholderFunction(...))[-1], 
            function(expr) {
                d <- deparse(expr)
                paste(d, collapse = "\n        ")
            }
        )
        if (is.null(argTags)) argTags <- rep("", n)
        namedArgs <- ifelse(argTags != "", argTags, deparsedArgs)
        cat(paste(namedArgs, evaluatedArgs, sep=" = "), sep = "\n")
    }

It works fine on your examples,

    my_names <- c("Bill", "William")
    display(x=log(10), 1+2+3, sin(1), rev(my_names),
            z=(function(p){lp<-log(p);lp+lp^2/2+lp^3/6})(0.2))
    # x = 2.30258509299405
    # 1 + 2 + 3 = 6
    # sin(1) = 0.841470984807897
    # rev(my_names) = c("William", "Bill")
    # z = -1.00911130949159

while there still are problems with matrices (and probably other structures):

    A <- matrix(1:4, 2, 2); B=diag(4)
    display(A, B)
    # A = 1:4
    # B = c(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)

I guess I have to define my own output representation in these cases, based
on "lapply(evaluatedArgs, class)".

Many thanks, Hans Werner