Can the print() command in R be quieted? - r

In R some functions can print information and return values, can the print be silenced?
For example:
print.and.return <- function() {
print("foo")
return("bar")
}
returns
> print.and.return()
[1] "foo"
[1] "bar"
>
I can store the return like:
> z <- print.and.return()
[1] "foo"
> z
[1] "bar"
>
Can I suppress the print of "foo"?

You may use hidden functional nature of R, for instance by defining function
deprintize<-function(f){
return(function(...) {capture.output(w<-f(...));return(w);});
}
that will convert 'printing' functions to 'silent' ones:
noisyf<-function(x){
print("BOO!");
sin(x);
}
noisyf(7)
deprintize(noisyf)(7)
deprintize(noisyf)->silentf;silentf(7)

?capture.output

If you absolutely need the side effect of printing in your own functions, why not make it an option?
print.and.return <- function(..., verbose=TRUE) {
if (verbose)
print("foo")
return("bar")
}
> print.and.return()
[1] "foo"
[1] "bar"
> print.and.return(verbose=FALSE)
[1] "bar"
>

I agree with hadley and mbq's suggestion of capture.output as the most general solution. For the special case of functions that you write (i.e., ones where you control the content), use message rather than print. That way you can suppress the output with suppressMessages.
print.and.return2 <- function() {
message("foo")
return("bar")
}
# Compare:
print.and.return2()
suppressMessages(print.and.return2())

I know that I might be resurrecting this post but someone else might find it as I did. I was interested in the same behaviour in one of my functions and I just came across "invisibility":
It has the same use of return() but it just don't print the value returned:
invisible(variable)
Thus, for the example given by #ayman:
print.and.return2 <- function() {
message("foo")
invisible("bar")
}

Related

In R, how do I define a function which is equivalent to `deparse(substitute(x))`?

I want to write a function in R which grabs the name of a variable from the context of its caller's caller. I think the problem I have is best understood by asking how to compose deparse and substitute. You can see that a naive composition does not work:
# a compose operator
> `%c%` = function(x,y)function(...)x(y(...))
# a naive attempt to combine deparse and substitute
> desub = deparse %c% substitute
> f=function(foo) { message(desub(foo)) }
> f(log)
foo
# this is how it is supposed to work
> g=function(foo) { message(deparse(substitute(foo))) }
> g(log)
log
I also tried a couple of variations involving eval.parent but with no luck. Any help is appreciated.
Clarification: I'm not looking for a synonym for deparse(substitute(...)), e.g. match.call()[[2]] - what I'm looking for is a way to define a function
desub = function(foo) {
...
# What goes here?
}
such that the definition of f above produces the same answer as g. It should look like this:
> f=function(foo) { message(desub(foo)) }
> f(log)
log
Perhaps match.call could be of use in the body of desub above, but I'd like to know how. Thanks!
As you surmised, this is an issue with environments. The reason why the function f does not give log when you call f(log), is that the environment in which substitute is called, namely the evaluation environment of desub, does not contain a binding to log.
The remedy is to evaluate the call to substitute in the proper environment, and modify desub accordingly:
desub <- function(x, env = parent.frame()) {
deparse(eval(substitute(substitute(x)), envir = env))
}
Now f does what it was intended to do:
f(log)
#> log
Thanks to #egnha and #akrun for the brave attempts. After playing around a bit I found a solution that works.
This fragment:
desub <- function(y) {
e1=substitute(y)
e2=do.call(substitute,list(e1), env=parent.frame())
deparse(e2)
}
gives:
> f <- function(x) message(desub(x))
> f(log)
log
Update:
With help from Mark Bravington on the R-devel list, I was able to generalize this to multiple frames. I thought I should post it here, because it's a bit more useful than the above, and because there was a tricky workaround involving (possibly buggy?) behavior in parent.frame().
# desub(v,0)=="v"
# desub(v,1)==deparse(substitute(v))
# desub(v,2)==name of v in grandparent's frame
# etc.
desub = function(y,n=1) {
env=environment();
for(i in 0:n) {
y = do.call(substitute, list(substitute(y)), env=env)
env = do.call(my_mvb_parent, list(), env=env)
}
deparse(y)
}
# helper:
#
# - using mvb.parent.frame fixes problems with capture.output and
# weird cycling behavior in the built-in parent.frame
#
# - this wrapper makes mvb.parent.frame not throw an error when we get
# to globalenv()
my_mvb_parent=function() {
library(mvbutils)
tryCatch(
mvb.parent.frame(2),
error=function(e) { globalenv()})
}
if(1) {
# example code
g2=function(t) {
for(i in 0:5) {
res=desub(t,i);
print(res);
res1=capture.output(desub(t,i))
stopifnot(capture.output(res)==res1)
}
}
g1=function(z) g2(z)
g=function(y) g1(y)
g(log)
# prints:
## [1] "t"
## [1] "z"
## [1] "y"
## [1] "log"
## [1] "log"
## [1] "log"
}

Creating dynamically methods in R6Class, magic of print(ls.str()) [duplicate]

This question already has answers here:
Explain a lazy evaluation quirk
(2 answers)
Closed 7 years ago.
In R, I wanted to create a class (R6Class) that when calling initialize creates few dynamic methods (the number of methods and its names depends on parameters in initialize). But I get into strange problem with environments.
Here is a simplified version of code that doesn't work.
library(R6)
ffactory <- function(i) {
function() i
}
A <- R6Class(
lock_objects=FALSE,
public=list(
initialize=function(args) {
for (i in args) {
self[[i]] <- ffactory(i)
}
}
)
)
a <- A$new(c('a', 'b', 'c'))
Now:
> a$a()
[1] "c"
> a$b()
[1] "c"
> a$c()
[1] "c"
In order to find what was wrong I had added a line that prints environment in ffactory function. That is
ffactory <- function(i) {
print(ls.str())
function() i
}
And now it has started to work!!!
> a$a()
[1] "a"
> a$b()
[1] "b"
> a$c()
[1] "c"
So why? There should be something I don't understand. Observer effect or what? :)
What is the magic of the line print(ls.str())? Actually I cannot remove neither print nor str from this line. Of course it is so silly to have a line like that. Not to mention garbage on the screen.
You have encountered lazy evaluation - R waits as long as it is able to before evaluating i - and in the former case, i will be evaluated at its last value in all instances. There's nothing really special about the combination of print and ls.str; anything that forces i to be evaluated prior to your method calls (a$a(), a$b(), etc...) will do the same.
Formally, this is what force is used for:
ffactory <- function(i) {
force(i);
function() i
}
R> a$a()
#[1] "a"
R> a$b()
#[1] "b"
R> a$c()
#[1] "c"
However, this also happens to do the job:
ffactory <- function(i) {
#force(i);
.z <- capture.output(cat(i, "\n"))
function() i
}
R> a$a()
#[1] "a"
R> a$b()
#[1] "b"
R> a$c()
#[1] "c"
There are presumably countless ways to force evaluation; I would argue that using force makes your intention most clear, though.
Quoting the help file directly,
force forces the evaluation of a formal argument. This can be useful
if the argument will be captured in a closure by the lexical scoping
rules and will later be altered by an explicit assignment or an
implicit assignment in a loop or an apply function.
and subsequently,
This is semantic sugar: just evaluating the symbol will do the same
thing (see the examples).
In fact, looking at how force is defined,
R> force
#function (x)
# x
#<bytecode: 0x3b7b528>
#<environment: namespace:base>
You could even get away with
ffactory <- function(i) {
i; function() i
}
But as noted, I think the explicitly calling force will make your code more readable.

Saving output of a recursive function as a list

I'd like to save the output of my recursive function as a list.
> (getNames(74, foo))
[1] "Excavata"
[1] "Excavata"
[1] "Excavata"
[1] "Excavata"
[1] "Stramenopiles"
[1] "Stramenopiles"
[1] "Stramenopiles"
[1] "Excavata"
[1] "Excavata"
[1] "Metazoa"
How do I go about doing this? Maybe this is simpler than I think but I've been stuck for a couple of days!
I don't think the code here matters: the general idea of my question regards saving the output of a recursive function (such as towers of hanoi) as a data type such as a list or a data.frame.
If you really think the exact code doesn't matter:
recursive_add <- function(x, res_list=NULL) {
if (is.null(res_list)) {
res_list <- list()
}
if (x == 26) {
return(res_list)
}
res_list[[x]] <- letters[x]
res_list <- recursive_add(x + 1, res_list)
return(res_list)
}
recursive_add(1)
The default NULL argument allows you to create a new list the first time, but then pass the existing list down to the recursive calls.
Without code to see I'm not sure as to whether it would be useful, but have you considered using rapply? rapply help page its a recursive version of lapply and lapply returns lists as output.

force-evaluate an object being passed as an argument to a function

I would like to pass the value of an object as an argument to a function.
# This is my object
anObject <- "an_unkown_string"
# I would like to do the equivalent of:
someFunc("an_unkown_string")
# .. by somehow calling on the object containing the string
someFunc( ??? (anObject) )
For example, with the sample function below (based on save()):
someFunc <- function(...) {
names <- as.character(substitute(list(...)))[-1L]
return(names)
}
# Ideally, the output would be:
someFunc( ??? (anObject) )
[1] "an_unkown_string"
I do not have access to modify someFunc
I have tried the following, but with no success.
someFunc(Name_of_Object)
someFunc(eval(Name_of_Object))
someFunc(evalq(Name_of_Object))
someFunc(force(Name_of_Object))
someFunc(eval(parse(text=Name_of_Object)))
Any help is appreciated.
How about
> do.call(someFunc, list(anObject))
[1] "an_unkown_string"
Or you could make a wrapper
myWrap <- function(...) {
do.call(someFunc, as.list(...))
}
> myWrap(anObject)
[1] "an_unkown_string"
Another way to construct a call and evaluate it:
> call("someFunc", anObject)
someFunc("an_unkown_string")
> eval(call("someFunc", anObject))
[1] "an_unkown_string"
I suppose I should mention that ?do.call says
The behavior of some functions, such as substitute, will not be the same for functions evaluated using do.call as if they were evaluated from the interpreter. The precise semantics are currently undefined and subject to change.
Nevertheless, for now at least, anObject is evaluated when the call is constructed (in the call to call or do.call), so substitute finds "an_unknown_string" instead of "anObject".
I'm puzzled. Why are you trying to make this more complex than it realy is?
someFunc <- function(obj) {
return(obj)
}
> someFunc(anObject)
[1] "an_unkown_string"

Why is my function looking for an input with the exact same name instead of working with the variable I passed?

test is a function to check if an object exists in the global environment, is not empty, and belongs to a particular class.
test <- function(foo, response=TRUE) {
if (missing(foo)) {
response <- FALSE
}
if (response) {
if (!exists(as.character(substitute(foo)), envir = .GlobalEnv)) {
response <- FALSE
}
}
if (response) {
response <- ifelse(class(foo) != "numeric", FALSE, TRUE)
}
return(response)
}
Now in foobar and a dozen other functions, I want to make sure foo is the right kind of object I want before proceeding with anything else.
foobar <- function(foo)
{
if(test(foo)) {
cat ("Yes, I have foo! \n")
}
if(!test(foo)) {
cat("Sorry, not a valid foo")
}
}
>ls()
[1] "foobar" "test"
>test(a)
[1] FALSE
>a <- "foobar"
>test(a)
[1] FALSE
>a <- 1
>test(a)
[1] TRUE
>foobar(a)
Sorry, not a valid foo
>
# what the???
>ls()
[1] "a" "foobar" "test"
>foo <- 1
>foobar(foo)
Yes, I have foo!
>
Objects loose their original names when handed off more than once. The copies get assigned new localnames. You need to grab the name on the first pass and then test with ls()
foobar <- function(foo)
{ fooname <- deparse(substitute(foo)); print(fooname)
if(test(fooname) ) {
cat ("Yes, I have foo! \n")
}
if(!test(fooname) ) {
cat("Sorry, not a valid foo")
}
}
test <- function(foo, response=TRUE) {
if (missing(foo)) {
response <- FALSE
}
if (response) {
if ( foo %in% ls( envir = .GlobalEnv) ) {
response <- TRUE }else {response <- FALSE}
}
return(response)
}
foobar(after)
# [1] "after"
#Yes, I have foo!
To verify, the problem is in the fact that the substitute is nested.
When R looks at foobar(a), it runs test(foo) within the foobar function, and so the variable that the test function looks at is called foo.
I'll start with a toy example to make things easier to explain. The library function, like your test function, interprets its argument via the variable name. i.e. library(MASS) loads the 'MASS' library, not the string that is contained inside the variable 'MASS'.
Now I'll make a function f that just calls library - this mirrors your foobar function:
f <- function(x) {
library(x)
}
Now let's try:
> f(MASS)
Error in library(x) : there is no package called ‘x’
Oh no! It didn't work! How come? Because remember, within the library code, it substitutes the variable passed in. i.e. library <- function(lib,...) substitute(lib).
So f(MASS) goes to function(x) library(x), and hence it's like I typed library(x) straight into the command line -- library is only trying to load x, and not x's value, MASS.
OK, we can fix this: we just need to change library(x) to library(substitute(x)), since substitute(x) is MASS and we'll then end up with library(MASS), right?
f <- function(x) {
library(substitute(x))
}
Let's try:
> f(MASS)
Error in library(substitute(x)) : 'package' must be of length 1
Urgh, what happened? Within f, the substitute(x) is not being evaluated, because library purposefully doesn't evaluate the expression it gets fed, because then typing library(MASS) in the command line wouldn't work.
So we really want to save substitute(x) as a variable and then perform library on that variable.
The only problem is that even if we do y <- substitute(x); library(y) within f, we always run into this problem that the argument fed into library is never evaluated. So doing this will cause the same as the first error: 'there is no package called y'.
How can we fix this? We need to somehow indirectly call library with substitute(x) as the argument, where substitute(x) is evaluated.
Aha! We can use do.call! (note: I didn't come up with this on my own, I was guided by this post to the R mailing list on nested substitutes:
f <- function(x) {
do.call(library,list(substitute(x)))
}
This does exactly what we want - it calls library but passes substitute(x) in as the library. It evaluates substitute(x) first, since we haven't directly written library(substitute(x)). nifty right?
> f(MASS) # no error!
# see if MASS is loaded - check if function 'lda' is there:
> exists('lda',mode='function')
[1] TRUE # huzzah!
Solution for your case
So, applying this lesson to your question, try:
foobar <- function(foo)
{
if ( do.call(test,list(substitute(foo))) ) # see the do.call & substitute?
cat ("Yes, I have foo! \n")
else
cat("Sorry, not a valid foo")
}
Let's see:
> ls()
[1] "foobar" "test"
> test(a)
[1] FALSE
> a <- 'foobar'
> test(a)
[1] FALSE
> a <- 1
> test(a)
[1] TRUE
> foobar(a)
Yes, I have foo!
Huzzah! (by the way: thanks for asking this question, because the answer is something I've always wanted to know).

Resources