简体   繁体   English

R.exe 和 Rscript.exe

[英]R.exe and Rscript.exe

I'm new to programming and I'm confused on the difference between the two.我是编程新手,我对两者之间的区别感到困惑。 I have googled this and I am still confused on the difference after reading the responses.我在谷歌上搜索过这个,在阅读了回复后,我仍然对差异感到困惑。

Part of the reason I am confused is I am thinking in terms of running script in Batch files.我感到困惑的部分原因是我正在考虑在批处理文件中运行脚本。 For instance, lets say I have a script in R and I create a batch file that runs the script where I use R.exe.例如,假设我在 R 中有一个脚本,我创建了一个批处理文件来运行我使用 R.exe 的脚本。 When I put this in the command prompt and run the batch file, it just takes the script I made and runs it in the console of R right?当我把它放在命令提示符中并运行批处理文件时,它只需要我制作的脚本并在 R 的控制台中运行它,对吗?

I've seen that you can run batch files uses Rscript.exe, which confuses me because when if I take an R script I made and put it into the script part of R (above the console) how would this do anything since the script must be put into the console for it to run.我已经看到您可以使用 Rscript.exe 运行批处理文件,这让我感到困惑,因为当我使用我制作的 R 脚本并将其放入 R 的脚本部分(控制台上方)时,这将如何做任何事情,因为脚本必须放入控制台才能运行。 (Unless Rscript.exe runs whatever it is in the script part of R?) (除非 Rscript.exe 运行 R 脚本部分中的任何内容?)

If anyone could please explain how this all works to me, I would greatly appreciate it.如果有人可以向我解释这一切是如何运作的,我将不胜感激。 Thanks!谢谢!

First, some terminology: even though the concept of batch processing is generic, and it means unassisted execution , the term batch file is usually reserved for MS-Windows files processed by cmd.exe, MS-Windows traditional script files.首先,一些术语:尽管批处理的概念是通用的,它意味着无辅助执行,但术语批处理文件通常保留给 cmd.exe 处理的 MS-Windows 文件,MS-Windows 传统脚本文件。 The term used for files containing R commands is usually R scripts , or Rscripts .用于包含R命令的文件的术语通常是R 脚本Rscripts

That said, please consider the following simple R script, named HelloFriend.R :也就是说,请考虑以下名为HelloFriend.R简单 R 脚本:

my.name <- readline(prompt="Enter name: ")
print(paste("Hello, ", my.name, "!"))

When run directly in R console, as当直接在 R 控制台中运行时,如

> source('HelloFriend.R')

it will show the output它会显示输出

Enter name: 

If the user types Some Name and hits Enter , the program will output如果用户Enter Some Name并按Enter ,程序将输出

[1] "Hello, Some Name !"

If it's run in the command line as R --no-save --quiet < HelloFriend.R , it will generate the output如果它在命令行中作为R --no-save --quiet < HelloFriend.R ,它将生成输出

> my.name <- readline(prompt="Enter name: ")
Enter name: 
> print(paste("Hello, ", my.name, "!"))
[1] "Hello,   !"
> 

And finally, if run with Rscript --vanilla HelloFriend.R , it will generate the output最后,如果使用Rscript --vanilla HelloFriend.R运行,它将生成输出

Enter name: 
[1] "Hello,   !"

In other words, when run inside the R console, the user input will be expected.换句话说,当在R控制台内运行时,将需要用户输入。 When run under R , but in the command line, the program will not give the user the opportunity to type anything, but the command echo will be shown.R下运行时,但在命令行中,程序不会给用户输入任何内容的机会,但会显示命令 echo。

And finally, under Rscript , the user input will also not be expected, but the command echo will not be shown.最后,在Rscript下,也不会预期用户输入,但不会显示命令 echo。

Rscript is the preferred form of running R scripts, as its name suggests. Rscript是运行 R 脚本的首选形式。 The passing of R scripts in the command line to R via redirection also gives batch processing but will echo the commands executed.通过重定向将命令行中的 R 脚本传递给 R 也提供了批处理,但会回显执行的命令。 Therefore it can help debug code, but it's not the preferred way of executing production code.因此它可以帮助调试代码,但它不是执行生产代码的首选方式。

The analogy with batch files is a good one.与批处理文件的类比是一个很好的类比。 R.exe is for interacting with the language, entering one statement at a time, and evaluating the results before entering the next statement. R.exe用于与语言交互,一次输入一个语句,并在输入下一个语句之前评估结果。 Rscript.exe is for running an existing script (file) containing R commands. Rscript.exe用于运行包含 R 命令的现有脚本(文件)。 You generally invoke Rscript.exe with the name of the script.您通常使用脚本名称调用Rscript.exe

Running Rscript.exe my_script.R from the command-line is sort of like running Rscript.exe my_script.R运行Rscript.exe my_script.R有点像运行

source("my_script.R")
q("no")

from the R console.从 R 控制台。

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

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