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

从命令行运行 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条)

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作