如何测试R是否以Rscript运行? [英] How do I test if R is running as Rscript?

查看:247
本文介绍了如何测试R是否以Rscript运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个R文件中有一个代码,我希望能够在开发过程中在RStudio中获得(即定义我的函数等)信息,并且还可以通过命令行使用#! /usr/bin/env Rscript语法运行(实际上,使用Hadoop).对于后者,我需要Rscript要做的最后一件事是开始分析(即,使用对main()函数的调用).对于前者,我不想调用main()函数.我希望能够测试代码是否在Rscript(或RStudio)中运行,以便我可以执行main()或不执行.可以吗?

I have code in a single R file that I want to be able to source (i.e., to define my functions etc.) within RStudio during development, and also run using the #! /usr/bin/env Rscript syntax via the command line (actually, using Hadoop). For the latter, I need the last thing that Rscript does to be to kick off the analysis (i.e., using a call to a main() function). For the former, I don't want my main() function called. I'd like to be able to test if the code is running within Rscript (or, alternatively, within RStudio), so that I can either execute main() or not. Is this possible, please?

一种解决方案是将我的代码分成多个文件,但是我宁愿避免这种情况(以使Hadoop的工作稍微容易一些).

One solution would be to break my code into multiple files, but I'd rather avoid this if possible (to make the Hadoop stuff slightly easier).

谢谢.

推荐答案

您可以使用interactive测试R是否在交互模式下运行. interactive将在Rscript下返回FALSE,并在(大多数?)GUI下返回TRUE.

You could use interactive to test if R is running in interactive mode. interactive will return FALSE under Rscript and TRUE under (most?) GUIs.

这篇关于如何测试R是否以Rscript运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆