如何避免每次运行时都打印R脚本?

时间:2022-02-07 04:19:46

Suppose I have an R script:

假设我有一个R脚本:

library('nnet')    
something <- runif(50); 
print(something) 

When I run this script from the command line, it prints:

当我从命令行运行此脚本时,它将输出:

> library('nnet')
> something <- runif(5); 
> print(something)
 [1] 0.04665518 0.93574275 0.96387299 0.07410239 0.92834019

I would like it to print only:

我只希望它印出来:

[1] 0.04665518 0.93574275 0.96387299 0.07410239 0.92834019

and I cannot figure out how to do this. sink("/dev/null") doesn't do anything, redirecting stderr manually doesn't do anything, and I can't find any useful information on this.

我不知道怎么做。sink(“/dev/null”)什么都不做,手动重定向stderr什么都不做,我找不到任何有用的信息。

5 个解决方案

#1


20  

Resolution is to run with Rscript, and not with R. Examples elsewhere (e.g. How can I read command line parameters from an R script?), run scripts from the command line with

解析是使用Rscript运行,而不是使用其他地方的R.示例(例如,如何从R脚本读取命令行参数?),从命令行运行脚本

R --args args1 args2... < foo.R

running with

运行与

Rscript foo.R args1 args2 ...

produces only the output, and not the script. It's also a much cleaner way to run scripts.

只生成输出,而不生成脚本。这也是一种更干净的运行脚本的方式。

#2


2  

Not an R user myself, but is this something that might be helpful to you? How can I run an 'R' script without suppressing output?

我自己不是R用户,但这对你有帮助吗?如何在不抑制输出的情况下运行'R'脚本?

From the linked question:

从相关的问题:

specify print.eval parameter set to TRUE if you want to get only the output (and not the commands). If you would need the commands too, you should set echo to TRUE (which implies setting print.eval to TRUE).

指定打印。如果您只想获得输出(而不是命令),则将eval参数设置为TRUE。如果您也需要这些命令,您应该设置echo为TRUE(这意味着设置打印)。eval为TRUE)。

For example:

例如:

source('myscript.R', print.eval = TRUE)

#3


2  

source( 'path/name/filnam.R' , verbose=FALSE)

#4


1  

For running in the terminal directly:

直接在终端运行:

R --slave --args dense 12 0.98 < transformMatrixToSparseMatrix.R

For running R script from Python:

从Python中运行R脚本:

process = subprocess.Popen(["R --slave --args %s %d %.2f < /path/to/your/rscript/transformMatrixToSparseMatrix.R" % (, 12, 0.98) ], shell=True)
process.wait()

See also this reference

参见这个引用

#5


0  

For RStudio IDE (Version 1.1.383) in Windows:

RStudio IDE(版本1.1.383)在Windows中:

Pressing Ctrl+Shift+Enter keys run entire script with echo (verbose)

按下Ctrl+Shift+Enter键,运行完整的echo脚本(冗长)

Pressing Ctrl+Shift+S keys run entire script without echo (non-verbose)

按Ctrl+Shift+S键运行整个脚本,没有echo(非verbose)

#1


20  

Resolution is to run with Rscript, and not with R. Examples elsewhere (e.g. How can I read command line parameters from an R script?), run scripts from the command line with

解析是使用Rscript运行,而不是使用其他地方的R.示例(例如,如何从R脚本读取命令行参数?),从命令行运行脚本

R --args args1 args2... < foo.R

running with

运行与

Rscript foo.R args1 args2 ...

produces only the output, and not the script. It's also a much cleaner way to run scripts.

只生成输出,而不生成脚本。这也是一种更干净的运行脚本的方式。

#2


2  

Not an R user myself, but is this something that might be helpful to you? How can I run an 'R' script without suppressing output?

我自己不是R用户,但这对你有帮助吗?如何在不抑制输出的情况下运行'R'脚本?

From the linked question:

从相关的问题:

specify print.eval parameter set to TRUE if you want to get only the output (and not the commands). If you would need the commands too, you should set echo to TRUE (which implies setting print.eval to TRUE).

指定打印。如果您只想获得输出(而不是命令),则将eval参数设置为TRUE。如果您也需要这些命令,您应该设置echo为TRUE(这意味着设置打印)。eval为TRUE)。

For example:

例如:

source('myscript.R', print.eval = TRUE)

#3


2  

source( 'path/name/filnam.R' , verbose=FALSE)

#4


1  

For running in the terminal directly:

直接在终端运行:

R --slave --args dense 12 0.98 < transformMatrixToSparseMatrix.R

For running R script from Python:

从Python中运行R脚本:

process = subprocess.Popen(["R --slave --args %s %d %.2f < /path/to/your/rscript/transformMatrixToSparseMatrix.R" % (, 12, 0.98) ], shell=True)
process.wait()

See also this reference

参见这个引用

#5


0  

For RStudio IDE (Version 1.1.383) in Windows:

RStudio IDE(版本1.1.383)在Windows中:

Pressing Ctrl+Shift+Enter keys run entire script with echo (verbose)

按下Ctrl+Shift+Enter键,运行完整的echo脚本(冗长)

Pressing Ctrl+Shift+S keys run entire script without echo (non-verbose)

按Ctrl+Shift+S键运行整个脚本,没有echo(非verbose)