如何在shell脚本中运行R代码? [英] How to run R codes inside shell script?
问题描述
我有一个 R 文件 (myfile.R).我想使用 shell 脚本运行它.我怎样才能做到这一点?我试过这个:
I have a R file ( myfile.R). I want to run it using a shell script. How Can I do that? I tried this:
#! /bin/bash
Rscript myfile.R
但它给了我这个错误:Rscript: command not found
but it gives me this error: Rscript: command not found
我也试过这个:
#! /bin/bash
R --not-save-- < myfile.R
它也给出了这个错误:R : command not found
It also gives this error: R : command not found
最好的方法是什么?
推荐答案
这个想法是不是写一个shell,而是写一个R脚本.这就是 Rscript
的用途,我们提供的 littler 包/usr/bin/r
甚至在此之前.
The idea is to not write a shell but to write an R script. That is what Rscript
is for, and our littler package offered /usr/bin/r
even before that.
就这样吧
#!/usr/bin/Rscript
cat("Hello, world\n")
# rest of your code below
或使用
#!/usr/bin/r
对于 littler -- 我有多个 cron 作业就是这样做的.
for littler -- I have multiple cron jobs doing just that.
这显然假设您在 /usr/bin
中有 Rscript
或 r
在常规 Debian 或 Ubuntu 机器上.
This obviously assumes that you'd have Rscript
or r
in /usr/bin
as you would e.g. on a regular Debian or Ubuntu box.
这篇关于如何在shell脚本中运行R代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!