在命令行(終端)上使用 R 腳本的最佳方式是什麼? (What's the best way to use R scripts on the command line (terminal)?)


問題描述

在命令行(終端)上使用 R 腳本的最佳方式是什麼? (What's the best way to use R scripts on the command line (terminal)?)

It's very convenient to have R scripts for doing simple plots from the command line.  However, running R from bash scripts is not convenient at all.  The ideal might be something like

#!/path/to/R
...

or

#!/usr/bin/env R
...

but I haven't been able to make either of those work.

Another option is keeping the scripts purely in R, e.g. script.R, and invoking it with R ‑‑file=script.R or similar.  However, occasionally a script will rely on obscure command line switches at which point part of the code exists outside the script.  Example: sneaking things into R from bash via a local .Rprofile, the desired switches are then everything ‑‑vanilla implies except ‑‑no‑init‑file.

Another option is a bash script to store the R flags and be painlessly executable, which then calls the R script.  The problem is that this means a single program just got split into two files which now have to be keep in sync, transferred to new machines together, etc.

The option I currently despise least is embedding the R in a bash script:

#!/bin/bash
... # usage message to catch bad input without invoking R
... # any bash pre‑processing of input
... # etc
R ‑‑random‑flags <<RSCRIPT
# R code goes here
RSCRIPT

Everything's in a single file.  It's executable and easily handles arguments.  The problem is that combining bash and R like this pretty much eliminates the possibility of any IDE not failing on one or the other, and makes my heart hurt real bad.

Is there some better way I'm missing?

‑‑‑‑‑

參考解法

方法 1:

Content of script.r:

<pre class="lang‑r prettyprint‑override">#!/usr/bin/env Rscript

args = commandArgs(trailingOnly = TRUE)
message(sprintf("Hello %s", args[1L]))
</code></pre>

The first line is the shebang line. It’s best practice to use /usr/bin/env Rscript instead of hard‑coding the path to your R installation. Otherwise you risk your script breaking on other computers.

Next, make it executable (on the command line):

<pre class="lang‑sh prettyprint‑override">chmod +x script.r </pre>

Invocation from command line:

<pre class="lang‑sh prettyprint‑override">./script.r world

 Hello world

</code></pre>

方法 2:

Try littler. littler provides hash‑bang (i.e. script starting with #!/some/path) capability for GNU R, as well as simple command‑line and piping use.

方法 3:

Miguel Sanchez's response is the way it should be. The other way executing Rscript could be 'env' command to run the system wide RScript.

#!/usr/bin/env Rscript

方法 4:

#!/path/to/R won't work because R is itself a script, so execve is unhappy.

I use R ‑‑slave ‑f script

方法 5:

If you are interested in parsing command line arguments to an R script try RScript which is bundled with R as of version 2.5.x

http://stat.ethz.ch/R‑manual/R‑patched/library/utils/html/Rscript.html

(by blahdiblahMiguel SanchezJouni K. SeppänenThe_Cute_HedgehogGary Capellphatrick)

參考文件

  1. What's the best way to use R scripts on the command line (terminal)? (CC BY‑SA 3.0/4.0)

#R #scripting #command-line #bash






相關問題

如何將均值、標準差等函數應用於整個矩陣 (How to apply mean, sd etc. function to a whole matrix)

Tạo các thùng của mỗi hàng trong bảng và vẽ hình thanh ngăn xếp trong R (Make bins of each table row and draw stack bar figure in R)

Reading not quite correct .csv file in R (Reading not quite correct .csv file in R)

包'treemap'中的線條粗細 (Thickness of lines in Package ‘treemap’)

是否需要帶有 awk 的預處理文件,或者可以直接在 R 中完成? (Is preprocessing file with awk needed or it can be done directly in R?)

rpivotTable 選擇元素下拉菜單 (rpivotTable select elements drop down menu)

優化性能 - Shiny 中的大文件輸入 (Optimizing Performance - Large File Input in Shiny)

數值取決於所應用的應用系列,R (Numeric values depending of apply family applied, R)

如何記錄全年的值? (How to note the values across year?)

R中的線性搜索 (Linear search in R)

在 dplyr/purrr 工作流程中動態連接多個數據集 (Dynamically join multiple datasets in a dplyr/purrr workflow)

如何將行值更改為列名 (R) (How change Row values to Column names (R))







留言討論