简体   繁体   English

从命令行运行 R 脚本

[英]Run R script from command line

I have a file, called ar , it has a chmod of 755,我有一个名为ar的文件,它的chmod为 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

Rscript a.R

Note that when using R CMD BATCH aR 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 aR ,将创建一个名为 a.Rout 的新文件,而不是将输出重定向到标准输出并在终端上显示。

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

One other thing to note about using Rscript is that it doesn't load the methods package by default which can cause confusion.关于使用 Rscript 需要注意的另一件事是默认情况下它不会加载methods包,这可能会导致混淆。 So if you're relying on anything that methods provides you'll want to load it explicitly in your script.因此,如果您依赖于方法提供的任何内容,您将希望在脚本中显式加载它。

If you really want to use the ./aR way of calling the script you could add an appropriate #!如果您真的想使用./aR调用脚本的方式,您可以添加适当的#! 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. It may be necessary to use littler to run shiny apps via a script?我还将注意到,如果您在 *unix 系统上运行,则有一个有用的littler包,它提供了到 R 的简单命令行管道。可能需要使用 littler 通过脚本运行闪亮的应用程序? Further details can be found in this question .可以在此问题中找到更多详细信息。

This does not answer the question directly.这并没有直接回答问题。 But someone may end up here because they want to run a oneliner of R from the terminal.但是有人可能会在这里结束,因为他们想从终端运行 R 的 oneliner。 For example, if you just want to install some missing packages and quit, this oneliner can be very convenient.例如,如果你只是想安装一些丢失的包并退出,这个oneliner可以很方便。 I use it a lot when I suddenly find out that I miss some packages, and I want to install them to where I want.当我突然发现我错过了一些软件包时,我经常使用它,我想将它们安装到我想要的地方。

  • To install to the default location:安装到默认位置:

     R -e 'install.packages(c("package1", "package2"))'
  • To install to a location that requires root privileges:要安装到需要root权限的位置:

     R -e 'install.packages(c("package1", "package2"), lib="/usr/local/lib/R/site-library")'

One more way of running an R script from the command line would be:从命令行运行 R 脚本的另一种方法是:

R < scriptName.R --no-save  

or with --save .或使用--save

See also What's the best way to use R scripts on the command line (terminal)?另请参阅在命令行(终端)上使用 R 脚本的最佳方法是什么? . .

You need the ?Rscript command to run an R script from the terminal.您需要?Rscript命令才能从终端运行 R 脚本。

Check out http://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rscript.html查看http://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rscript.html

Example例子

## example #! script for a Unix-alike

#! /path/to/Rscript --vanilla --default-packages=utils
args <- commandArgs(TRUE)
res <- try(install.packages(args))
if(inherits(res, "try-error")) q(status=1) else q()

How to run Rmd in command with knitr and rmarkdown by multiple commands and then Upload an HTML file to RPubs如何通过多个命令使用 knitr 和 rmarkdown 在命令中运行 Rmd,然后将 HTML 文件上传到 RPubs

Here is a example: load two libraries and run a R command这是一个示例:加载两个库并运行 R 命令

R -e 'library("rmarkdown");library("knitr");rmarkdown::render("NormalDevconJuly.Rmd")'

R -e 'library("markdown");rpubsUpload("normalDev","NormalDevconJuly.html")'

Yet another way to use Rscript for *Unix systems is Process Substitution .在 *Unix 系统上使用 Rscript 的另一种方法是Process Substitution

Rscript <(zcat a.r)
# [1] "hello"

Which obviously does the same as the accepted answer, but this allows you to manipulate and run your file without saving it the power of the command line, eg:这显然与接受的答案相同,但这允许您操作和运行文件而无需保存命令行的功能,例如:

Rscript <(sed s/hello/bye/ a.r)
# [1] "bye"

Similar to Rscript -e "Rcode" it also allows to run without saving into a file.Rscript -e "Rcode"类似,它还允许在不保存到文件的情况下运行。 So it could be used in conjunction with scripts that generate R-code, eg:因此它可以与生成 R 代码的脚本结合使用,例如:

Rscript <(echo "head(iris,2)")
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa

仅用于文档,有时您需要以sudo身份运行脚本:

sudo Rscript path/to/your/file.R

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM