从命令行运行R脚本 [英] Run R script from command line

查看:173
本文介绍了从命令行运行R脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,名为 ar ,它有一个 chmod 为755,

  sayHello<  -  function(){
print('hello')
}
$ b sayHello ()

如何通过命令行运行?


<如果要输出到终端打印,最好使用Rscript

  Rscript aR 

请注意,当使用 R CMD BATCH aR ,而不是将输出重定向到标准输出并在终端上显示一个名为a.Rout的新文件。

  R CMD BATCH aR 
#检查输出
cat a.Rout

如果你真的想使用 ./ aR 调用脚本的方式,你可以添加一个合适的#!到脚本的顶部

 #!/ usr / bin / env Rscript 
sayHello< function(){
print('hello')
}

sayHello()


I have a file, called a.r, it has a chmod of 755,

sayHello <- function(){
   print('hello')
}

sayHello()

How can I run this via command-line?

解决方案

If you want the output to print to the terminal it is best to use Rscript

Rscript a.R

Note that when using R CMD BATCH a.R that instead of redirecting output to standard out and displaying on the terminal a new file called a.Rout will be created.

R CMD BATCH a.R
# Check the output
cat a.Rout

If you really want to use the ./a.R way of calling the script you could add an appropriate #! to the top of the script

#!/usr/bin/env Rscript
sayHello <- function(){
   print('hello')
}

sayHello()

这篇关于从命令行运行R脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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