Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print if not assigned

Tags:

r

How can I write into a function a way to detect if the output is being assigned (<-) to something? The reasoning is I'd like to print a message if it is not being assigned and just goes to the console but if it is being assigned I'd like it not to print the message.

Here's a dummy example and how I'd like it to behave:

fun <- function(x) {
    if (being_assigned) {
        print("message")
    }
    return(x)
}

#no assignment so message prints 
> fun(6)  
[1] "message"
[1] 6

#assignment so message does not prints
> x <- fun(6)

The being_assigned in the function is the imaginary unknown condition I'd like to detect but don't know how.

like image 292
Tyler Rinker Avatar asked Oct 24 '12 19:10

Tyler Rinker


Video Answer


2 Answers

I think the best you can do is to define a special print method for objects returned by the function:

## Have your function prepend "myClass" to the class of the objects it returns
fun <- function(x) {
    class(x) <- c("myClass", class(x))
    x
}

## Define a print method for "myClass". It will be dispatched to 
## by the last step of the command line parse-eval-print cycle.
print.myClass <- function(obj) {
    cat("message\n")
    NextMethod(obj)
}

> fun(1:10)
message
 [1]  1  2  3  4  5  6  7  8  9 10
attr(,"class")
[1] "myClass"
>
> out <- fun(1:10)
> 
like image 200
Josh O'Brien Avatar answered Oct 13 '22 10:10

Josh O'Brien


I love Josh's idea but for future posters wanted to show what I did which is a slightly modified version of his approach. His approach prints out the class information which is the only thing I didn't like. He used the NextMethod to avoid the infinite recursion printing. This causes the

attr(,"class")
[1] "myClass"

to be printed. So to avoid this I print the message first and then print 1 through the length of the class object (using indexing).

fun <- function(x) {
    class(x) <- 'beep'
    comment(x) <- "hello world"
    return(x)
}

print.beep<- function(beep) {
    cat(paste0(comment(beep), "\n"))
    print(beep[1:length(beep)])
}


> fun(1:10)
hello world
 [1]  1  2  3  4  5  6  7  8  9 10

Thanks again Josh for the idea.

If the reader didn't want the little [1] index to print either they could cat the output int he print statement as:

print.beep<- function(beep) {
    cat(paste0(comment(beep), "\n"))
    cat(beep[1:length(beep)], "\n")
}
like image 33
Tyler Rinker Avatar answered Oct 13 '22 08:10

Tyler Rinker