Linux 如何在文件中为 R 编写和执行 hello world 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11502599/
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
How to write and execute a hello world program in file for R?
提问by Aquarius_Girl
I did look in here: http://cran.r-project.org/doc/FAQ/R-FAQ.html#R-Programming
我确实在这里查看过:http: //cran.r-project.org/doc/FAQ/R-FAQ.html#R-Programming
Wikipedia shows how to write an on the fly R program: http://en.wikipedia.org/wiki/Hello_world_program_examples#R_2
维基百科展示了如何编写动态 R 程序:http: //en.wikipedia.org/wiki/Hello_world_program_examples#R_2
But how to execute this from a file?
What extension needs to be given?
How to compile the file then?
但是如何从文件中执行它呢?
需要提供什么扩展?
那怎么编译文件呢?
on Linux.
在 Linux 上。
I created a file mow.R
containing the following code:cat ('Hello world!')
我创建了一个mow.R
包含以下代码的文件:cat ('Hello world!')
R says:
R 说:
> source mow.R
Error: unexpected symbol in "source mow.R"
采纳答案by Levon
I general you want to give your R files the .R
extension.
我一般你想给你的 R 文件.R
扩展名。
To run a program you can start R (by typing "R" at the command prompt) and once inside the program/interpreter, you can execute your program (let's call it "so.R"
) with the source command. E.g.,
要运行程序,您可以启动 R(在命令提示符下键入“R”),一旦进入程序/解释器,您就可以"so.R"
使用源命令执行您的程序(我们称之为)。例如,
> source('so.R')
yields:
产量:
Hello World
You can run the program from the Unix shell with
您可以从 Unix shell 运行该程序
R CMD BATCH so.R
it will generate a file named "so.Rout"
that will contain the output of your program run, especially if it contains non-trivial amounts of output. If there is a problem with the program run, the error messages etc will also be in this file so it's a good diagnostic tool. An alternative is the Rscriptcommand which sends its output to stdout, in which case if it's long you need to capture it yourself.
它将生成一个名为的文件"so.Rout"
,该文件将包含程序运行的输出,特别是如果它包含非平凡的输出量。如果程序运行出现问题,错误消息等也将在此文件中,因此它是一个很好的诊断工具。另一种选择是Rscript命令,它将其输出发送到 stdout,在这种情况下,如果它很长,您需要自己捕获它。
There is a very useful trick, when googling for R related topics it can be tricky because R is a single character. To be effective, pre-pend "r-help:" to your search terms. E.g.,
有一个非常有用的技巧,当在谷歌上搜索 R 相关主题时,它可能会很棘手,因为 R 是单个字符。为有效起见,请在您的搜索词前加上“ r-help:”。例如,
r-help:Running a program
r-help:Running a program
Here are two manuals that might be useful/help you get started:
以下是两本可能对您有用/帮助您入门的手册:
and also take a look at The R Manuals.
并查看The R Manuals。
More information/FAQs, etc., can be found at the R web siteitself. I have found that there is a lotof information on R (esp tutorials etc), but it can be tricky to find them. The "r-help" google trick really helps with this.
更多信息/常见问题等,可以在R 网站本身找到。我发现有很多关于 R 的信息(尤其是教程等),但要找到它们可能很棘手。“r-help”谷歌技巧确实对此有所帮助。
回答by johannes
You could also try:
你也可以试试:
Rscript mow.R
to get output on the console.
在控制台上获取输出。