如何在R的文件中编写和执行Hello World程序? [英] How to write and execute a hello world program in file for R?

查看:147
本文介绍了如何在R的文件中编写和执行Hello World程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实在这里查看: http://cran.r-project.org/doc/FAQ/R-FAQ.html#R-Programming

Wikipedia显示了如何实时编写R程序: http://en.wikipedia. org/wiki/Hello_world_program_examples#R_2

Wikipedia shows how to write an on the fly R program: 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?

在Linux上.

我创建了一个文件mow.R,其中包含以下代码:
cat ('Hello world!')

I created a file mow.R containing the following code:
cat ('Hello world!')

R说:

> source mow.R
Error: unexpected symbol in "source mow.R"

推荐答案

通常,您希望为R文件提供.R扩展名.

I general you want to give your R files the .R extension.

要运行程序,您可以启动R(通过在命令提示符下键入"R"),并且一旦进入程序/解释器,就可以使用source命令执行程序(将其称为"so.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.,

> source('so.R')

产量:

Hello World 

您可以使用以下命令从Unix shell运行程序:

You can run the program from the Unix shell with

R CMD BATCH so.R

它将生成一个名为"so.Rout"的文件,该文件将包含程序运行的输出,尤其是如果它包含大量的输出时.如果程序运行有问题,则此文件中也将包含错误消息等,因此它是一个很好的诊断工具.另一种方法是 Rscript 命令,该命令将其输出发送到stdout,在这种情况下,如果它很长您需要自己捕获它.

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 Rscript command which sends its output to stdout, in which case if it's long you need to capture it yourself.

有一个非常有用的技巧,在搜索R相关主题时可能会很棘手,因为R是单个字符.为有效起见,请在搜索词前添加" r-help:".例如,

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-help:Running a program

以下两本手册可能对您入门很有帮助/帮助:

Here are two manuals that might be useful/help you get started:

  • Introduction to R
  • Programming in R

并查看 R手册.

更多信息/常见问题等,可以在 R网站本身中找到.我发现R上有很多信息(esp教程等),但是很难找到它们. Google的"r-help"技巧确实可以解决这个问题.

More information/FAQs, etc., can be found at the R web site itself. I have found that there is a lot of information on R (esp tutorials etc), but it can be tricky to find them. The "r-help" google trick really helps with this.

这篇关于如何在R的文件中编写和执行Hello World程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆