如何在shell脚本中运行R代码? [英] How to run R codes inside shell script?

查看:110
本文介绍了如何在shell脚本中运行R代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 中有 Rscriptr在常规 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屋!

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