从命令行运行 R 代码 (Windows)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3506007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:32:23  来源:igfitidea点击:

Running R Code from Command Line (Windows)

windowscommand-liner

提问by ExtremeCoder

I have some R code inside a file called analyse.r. I would like to be able to, from the command line (CMD), run the code in that file without having to pass through the R terminal and I would also like to be able to pass parameters and use those parameters in my code, something like the following pseudocode:

我在名为 analysis.r 的文件中有一些 R 代码。我希望能够从命令行 (CMD) 运行该文件中的代码而不必通过 R 终端,我还希望能够传递参数并在我的代码中使用这些参数像下面的伪代码:

C:\>(execute r script) analyse.r C:\file.txt

and this would execute the script and pass "C:\file.txt" as a parameter to the script and then it could use it to do some further processing on it.

这将执行脚本并将“C:\file.txt”作为参数传递给脚本,然后它可以使用它对其进行进一步处理。

How do I accomplish this?

我该如何实现?

回答by Dirk Eddelbuettel

  1. You want Rscript.exe.

  2. You can control the output from within the script -- see sink()and its documentation.

  3. You can access command-arguments via commandArgs().

  4. You can control command-line arguments more finely via the getoptand optparsepackages.

  5. If everything else fails, consider reading the manualsor contributed documentation

  1. 你要Rscript.exe

  2. 您可以控制脚本内的输出——请参阅sink()及其文档。

  3. 您可以通过访问命令参数commandArgs()

  4. 您可以通过getoptoptparse包更精细地控制命令行参数。

  5. 如果其他一切都失败了,请考虑阅读手册提供的文档

回答by guns

Identify where R is install. For window 7 the path could be

确定 R 的安装位置。对于窗口 7,路径可能是

1.C:\Program Files\R\R-3.2.2\bin\x64>
2.Call the R code
3.C:\Program Files\R\R-3.2.2\bin\x64>\Rscript Rcode.r

回答by rupali

There are two ways to run a R script from command line (windows or linux shell.)

有两种方法可以从命令行(windows 或 linux shell)运行 R 脚本。

1) R CMD way R CMD BATCH followed by R script name. The output from this can also be piped to other files as needed.

1) R CMD 方式 R CMD BATCH 后跟 R 脚本名称。也可以根据需要将其输出通过管道传输到其他文件。

This way however is a bit old and using Rscript is getting more popular.

然而,这种方式有点老,使用 Rscript 越来越流行。

2) Rscript way (This is supported in all platforms. The following example however is tested only for Linux) This example involves passing path of csv file, the function name and the attribute(row or column) index of the csv file on which this function should work.

2)Rscript方式(所有平台都支持。但以下示例仅针对Linux进行测试)此示例涉及传递csv文件的路径,函数名称和csv文件的属性(行或列)索引功能应该可以工作。

Contents of test.csv file x1,x2 1,2 3,4 5,6 7,8

test.csv 文件内容 x1,x2 1,2 3,4 5,6 7,8

Compose an R file “a.R” whose contents are

编写一个 R 文件“aR”,其内容是

#!/usr/bin/env Rscript

cols <- function(y){
   cat("This function will print sum of the column whose index is passed from commandline\n")
   cat("processing...column sums\n")
   su<-sum(data[,y])
   cat(su)
   cat("\n")
}

rows <- function(y){
   cat("This function will print sum of the row whose index is passed from commandline\n")
   cat("processing...row sums\n")
   su<-sum(data[y,])
   cat(su)
   cat("\n")
}
#calling a function based on its name from commandline … y is the row or column index
FUN <- function(run_func,y){
    switch(run_func,
        rows=rows(as.numeric(y)),
        cols=cols(as.numeric(y)),
        stop("Enter something that switches me!")
    )
}

args <- commandArgs(TRUE)
cat("you passed the following at the command line\n")
cat(args);cat("\n")
filename<-args[1]
func_name<-args[2]
attr_index<-args[3]
data<-read.csv(filename,header=T)
cat("Matrix is:\n")
print(data)
cat("Dimensions of the matrix are\n")
cat(dim(data))
cat("\n")
FUN(func_name,attr_index)

Runing the following on the linux shell Rscript a.R /home/impadmin/test.csv cols 1 gives you passed the following at the command line /home/impadmin/test.csv cols 1 Matrix is: x1 x2 1 1 2 2 3 4 3 5 6 4 7 8 Dimensions of the matrix are 4 2 This function will print sum of the column whose index is passed from commandline processing...column sums 16

在 linux shell Rscript aR /home/impadmin/test.csv cols 1 上运行以下命令,您可以在命令行 /home/impadmin/test.csv cols 1 中传递以下内容:x1 x2 1 1 2 2 3 4 3 5 6 4 7 8 矩阵的维数为 4 2 此函数将打印其索引从命令行处理传递的列的总和...列总和 16

Runing the following on the linux shell Rscript a.R /home/impadmin/test.csv rows 2 gives you passed the following at the command line /home/impadmin/test.csv rows 2 Matrix is: x1 x2 1 1 2 2 3 4 3 5 6 4 7 8 Dimensions of the matrix are 4 2 This function will print sum of the row whose index is passed from commandline processing...row sums 7

在 linux shell Rscript aR /home/impadmin/test.csv rows 2 上运行以下命令,您可以在命令行 /home/impadmin/test.csv rows 2 中传递以下内容:x1 x2 1 1 2 2 3 4 3 5 6 4 7 8 矩阵的维数为 4 2 此函数将打印其索引从命令行处理传递的行的总和...行总和 7

We can also make the R script executable as follows (on linux) chmod a+x a.R and run the second example again as ./a.R /home/impadmin/test.csv rows 2

我们还可以使 R 脚本可执行如下(在 linux 上) chmod a+x aR 并再次运行第二个示例 ./aR /home/impadmin/test.csv rows 2

This should also work for windows command prompt..

这也适用于 Windows 命令提示符..

回答by Manideep Karthik

save the following in a text file

将以下内容保存在文本文件中

f1 <- function(x,y){
print (x)
print (y)
}
args = commandArgs(trailingOnly=TRUE)
f1(args[1], args[2])

No run the following command in windows cmd

没有在 windows cmd 中运行以下命令

Rscript.exe path_to_file "hello" "world"

This will print the following

这将打印以下内容

[1] "hello"
[1] "world"