零零乙 2013-08-19 04:23 采纳率: 33.3%
浏览 1146
已采纳

从命令行运行 r 脚本

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?

转载于:https://stackoverflow.com/questions/18306362/run-r-script-from-command-line

  • 写回答

7条回答 默认 最新

  • 笑故挽风 2013-08-19 05:01
    关注

    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()
    

    I will also note that if you're running on a *unix system there is the useful littler package which provides easy command line piping to R.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题