{R語言學(xué)習(xí)日記}Day4 hello world

大家好,我是William李梓峰,歡迎閱讀我的 R 語言學(xué)習(xí)日記。

官網(wǎng)鏈接:
https://www.tutorialspoint.com/r/r_basic_syntax.htm

As a convention, we will start learning R programming by writing a "Hello, World!" program. Depending on the needs, you can program either at R command prompt or you can use an R script file to write your program. Let's check both one by one.
按照慣例,我們先來寫個 R 語言的 "Hello, World!" 程序。你可以根據(jù)喜好來決定到底是直接使用 R 命令行窗口還是 R 腳本文件。不過,我們兩者都會介紹。

R Command Prompt

R 命令行窗口

Once you have R environment setup, then it’s easy to start your R command prompt by just typing the following command at your command prompt ?
一旦你已經(jīng)安裝了 R 語言環(huán)境,就可以非常輕易地啟動你的 R 命令行窗口了,只需在命令行那里敲下 R:

$ R

This will launch R interpreter and you will get a prompt > where you can start typing your program as follows ?
敲下 R 以后會立即啟動 R 解釋器,然后你就可以在 > 后面輸入 R 代碼了:

> myString <- "Hello, World!"
> print ( myString)
[1] "Hello, World!"

Here first statement defines a string variable myString, where we assign a string "Hello, World!" and then next statement print() is being used to print the value stored in variable myString.
上面的代碼中,第一行表示定義了一個字符串變量 myString,并且給它賦值了字符串 “Hello, World!”,然后在第二行代碼中打印它。

R Script File

R 腳本文件

Usually, you will do your programming by writing your programs in script files and then you execute those scripts at your command prompt with the help of R interpreter called Rscript. So let's start with writing following code in a text file called test.R as under ?
通常來說,你會直接在腳本文件中編程,然后你會通過一個叫 Rscript 的 R 解釋器在命令行窗口中執(zhí)行這些腳本。接下來,讓我們開始在一個名為 test.R 的文件中編寫 R 代碼吧:

# My first program in R Programming
myString <- "Hello, World!"

print ( myString)

Save the above code in a file test.R and execute it at Linux command prompt as given below. Even if you are using Windows or other system, syntax will remain same.
保存上面的代碼,然后執(zhí)行這個文件。即便你在 Windows 或其他系統(tǒng),命令都是一樣的:

$ Rscript test.R 

When we run the above program, it produces the following result.
當(dāng)我們運行上面的程序的時候,它就給出了下面的結(jié)果:

[1] "Hello, World!"

Comments

R 語言的注釋

Comments are like helping text in your R program and they are ignored by the interpreter while executing your actual program. Single comment is written using # in the beginning of the statement as follows ?
注釋就像是一個提示文本,它們總會被解釋器所忽略掉。單行注釋可以用 # 來表示:

# My first program in R Programming

R does not support multi-line comments but you can perform a trick which is something as follows ?
R 語言不支持多行注釋,但是你可以像下面這么操作來達(dá)到多行注釋的效果:

if(FALSE) {
   "This is a demo for multi-line comments and it should be put inside either a single
      OR double quote"
}

myString <- "Hello, World!"
print ( myString)

Though above comments will be executed by R interpreter, they will not interfere with your actual program. You should put such comments inside, either single or double quote.
雖然上面的注釋會被 R 解釋器執(zhí)行,但它們不會影響你程序的邏輯。你可以隨便扔些文本進(jìn)去,用單引號雙引號都行。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容