Can the print() command in R be quieted?

后端 未结 5 1948
故里飘歌
故里飘歌 2021-02-14 04:58

In R some functions can print information and return values, can the print be silenced?

For example:

print.and.return <- function() {
  print(\"foo\         


        
5条回答
  •  暖寄归人
    2021-02-14 05:21

    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)
    

提交回复
热议问题