[英]R command line best practices: exit, print stdout, print stderr, avoid warnings
Currently I run R scripts the following way: 目前我按以下方式运行R脚本:
R --slave < <script_fullname> argument1 argument2 ...
I was wondering the best practices in R on how to exit the script with a warning, would q() just do it? 我想知道R中关于如何通过警告退出脚本的最佳实践,q()会这样做吗?
if(!file.exists(argument1)){
q()
} }
print to the stdout 打印到标准输出
if(!file.exists(argument1)){
print('file does not exist')
q()
}
and print to the std err? 并打印到标准错误?
Also, I see the following warning everytime I run R scripts this way. 此外,每当我以这种方式运行R脚本时,我都会看到以下警告。 When reading the stdout I see:
阅读标准时,我看到:
ARGUMENT 'argument1' __ignored__
ARGUMENT 'argument2' __ignored__
Is there anyway to avoid such warnings? 反正有没有避免这样的警告?
If you are going to exit your R script because of an error, I suggest using 如果由于错误而要退出R脚本,我建议使用
stop("warning message here")
rather than print()
and q()
. stop("warning message here")
而不是print()
和q()
。
print()
is not recommended, since "it's hard to capture and selectively ignore this sort of output. Printed output is not a condition, so you can't use any of the useful condition handling tools." 不建议使用
print()
,因为“很难捕获并有选择地忽略这种输出。打印输出不是条件,所以你不能使用任何有用的条件处理工具。” -- from Debugging, condition handling, and defensive programming - 来自调试,条件处理和防御性编程
stderr is discussed in this SO post (too long to repeat all of the options here). 在这篇SO帖子中讨论了stderr(这里重复所有选项的时间太长)。
Try using --args
to avoid the warnings: 尝试使用
--args
来避免警告:
R --slave < <script_fullname> --args argument1 argument2 ...
See also Passing Custom Arguments . 另请参阅传递自定义参数 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.