在R中抑制一个命令的输出

时间:2022-02-24 20:11:29

I'm looking to suppress the output of one command (in this case, the apply function).

我希望抑制一个命令的输出(在本例中是apply函数)。

Is it possible to do this without using sink()? I've found the described solution below, but would like to do this in one line if possible.

是否可以不使用sink()进行此操作?我已经找到了下面描述的解决方案,但是如果可能的话,我想用一行来完成。

How to suppress output

如何抑制输出

6 个解决方案

#1


69  

It isn't clear why you want to do this without sink, but you can wrap any commands in the invisible() function and it will suppress the output. For instance:

不清楚为什么要在不使用sink的情况下执行此操作,但是可以将任何命令包装到hidden()函数中,它将抑制输出。例如:

1:10 # prints output
invisible(1:10) # hides it

Otherwise, you can always combine things into one line with a semicolon and parentheses:

否则,您总是可以将内容合并为一行,并使用分号和圆括号:

{ sink("/dev/null"); ....; sink(); }

#2


44  

Use the capture.output() function. It works very much like a one-off sink() and unlike invisible(), it can suppress more than just print messages. Set the file argument to /dev/null on UNIX or NUL on windows. For example, considering Dirk's note:

使用capture.output()函数。它的工作方式非常类似于一次性的sink(),与invisible()不同的是,它可以抑制的不仅仅是打印消息。将文件参数设置为UNIX上的/dev/null或windows上的NUL。例如,考虑到Dirk的注释:

> invisible(cat("Hi\n"))
Hi

> capture.output( cat("Hi\n"), file='NUL')
> 

#3


9  

R only automatically prints the output of unassigned expressions, so just assign the result of the apply to a variable, and it won't get printed.

R只会自动打印未赋值表达式的输出,所以只需将apply的结果分配给一个变量,它就不会被打印出来。

#4


5  

you can use 'capture.output' like below. This allows you to use the data later:

您可以使用“捕捉。下面的输出的像。这允许您以后使用这些数据:

log <- capture.output({
  test <- CensReg.SMN(cc=cc,x=x,y=y, nu=NULL, type="Normal")
})

test$betas

#5


1  

The following function should do what you want exactly:

下面的函数应该做你想做的事情:

hush=function(code){
  sink("NUL") # use /dev/null in UNIX
  tmp = code
  sink()
  return(tmp)
}

For example with the function here:

例如这里的函数:

foo=function(){
  print("BAR!")
  return(42)
}

running

运行

x = hush(foo())

Will assign 42 to x but will not print "BAR!" to STDOUT

将分配42到x,但不会打印“BAR!”到STDOUT

Note than in a UNIX OS you will need to replace "NUL" with "/dev/null"

注意,与UNIX操作系统相比,您需要将“NUL”替换为“/dev/null”

#6


0  

invisible(cat("Dataset: ", dataset, fill = TRUE))
invisible(cat(" Width: " ,width, fill = TRUE))
invisible(cat(" Bin1:  " ,bin1interval, fill = TRUE))
invisible(cat(" Bin2:  " ,bin2interval, fill = TRUE))
invisible(cat(" Bin3:  " ,bin3interval, fill = TRUE))

produces output without NULL at the end of the line or on the next line

生成在行末尾或下一行上没有NULL的输出

Dataset:  17 19 26 29 31 32 34 45 47 51 52 59 60 62 63
Width:  15.33333

Bin1:   17 32.33333
Bin2:   32.33333 47.66667
Bin3:   47.66667 63

#1


69  

It isn't clear why you want to do this without sink, but you can wrap any commands in the invisible() function and it will suppress the output. For instance:

不清楚为什么要在不使用sink的情况下执行此操作,但是可以将任何命令包装到hidden()函数中,它将抑制输出。例如:

1:10 # prints output
invisible(1:10) # hides it

Otherwise, you can always combine things into one line with a semicolon and parentheses:

否则,您总是可以将内容合并为一行,并使用分号和圆括号:

{ sink("/dev/null"); ....; sink(); }

#2


44  

Use the capture.output() function. It works very much like a one-off sink() and unlike invisible(), it can suppress more than just print messages. Set the file argument to /dev/null on UNIX or NUL on windows. For example, considering Dirk's note:

使用capture.output()函数。它的工作方式非常类似于一次性的sink(),与invisible()不同的是,它可以抑制的不仅仅是打印消息。将文件参数设置为UNIX上的/dev/null或windows上的NUL。例如,考虑到Dirk的注释:

> invisible(cat("Hi\n"))
Hi

> capture.output( cat("Hi\n"), file='NUL')
> 

#3


9  

R only automatically prints the output of unassigned expressions, so just assign the result of the apply to a variable, and it won't get printed.

R只会自动打印未赋值表达式的输出,所以只需将apply的结果分配给一个变量,它就不会被打印出来。

#4


5  

you can use 'capture.output' like below. This allows you to use the data later:

您可以使用“捕捉。下面的输出的像。这允许您以后使用这些数据:

log <- capture.output({
  test <- CensReg.SMN(cc=cc,x=x,y=y, nu=NULL, type="Normal")
})

test$betas

#5


1  

The following function should do what you want exactly:

下面的函数应该做你想做的事情:

hush=function(code){
  sink("NUL") # use /dev/null in UNIX
  tmp = code
  sink()
  return(tmp)
}

For example with the function here:

例如这里的函数:

foo=function(){
  print("BAR!")
  return(42)
}

running

运行

x = hush(foo())

Will assign 42 to x but will not print "BAR!" to STDOUT

将分配42到x,但不会打印“BAR!”到STDOUT

Note than in a UNIX OS you will need to replace "NUL" with "/dev/null"

注意,与UNIX操作系统相比,您需要将“NUL”替换为“/dev/null”

#6


0  

invisible(cat("Dataset: ", dataset, fill = TRUE))
invisible(cat(" Width: " ,width, fill = TRUE))
invisible(cat(" Bin1:  " ,bin1interval, fill = TRUE))
invisible(cat(" Bin2:  " ,bin2interval, fill = TRUE))
invisible(cat(" Bin3:  " ,bin3interval, fill = TRUE))

produces output without NULL at the end of the line or on the next line

生成在行末尾或下一行上没有NULL的输出

Dataset:  17 19 26 29 31 32 34 45 47 51 52 59 60 62 63
Width:  15.33333

Bin1:   17 32.33333
Bin2:   32.33333 47.66667
Bin3:   47.66667 63