• 编写代码的文本编辑器
• 运行此代码的地方

# Rstudio的主要组成部分

1. 控制台（红色窗格）
2. 环境（绿色窗格）
3. 文件，绘图，帮助等（蓝色窗格）

• 键入代码，然后按“RUN”按钮（见下文）或使用键盘快捷键CTRL + ENTER（CMD +在MAC上输入）。 只有块 然后将执行光标所在的代码。
• 键入您的代码，然后在文本编辑器中选择要执行的部件，然后按“运行”按钮或使用键盘快捷键Ctrl + Enter（CMD +在Mac上输入）。所有的 选择 代码将被执行

For example, try typing 1+1 in the text editor and execute it by clicking on “Run” (or CTRL/cmd + Enter). You should see the result 2 in the console, as shown in the screenshot below:

This means that you can now perform any computations with a, such that if you execute a + 1, RStudio will render 2 in the console. In this pane you can also see a tab with a history of the code executed and a button to import a dataset (more on 在RStudio导入数据集）。

• 阴谋: where you will see the rendered plots. For instance, run plot(1:10) and you should see it in this tab. If you plotted more than one plots, you can navigate between them by clicking on the arrows. You can open the plot in a new window by clicking on Zoom and export your plot by clicking on Export. Those buttons are located just under the Plot tab (see figure below)
• Packages: where you see all your installed packages. Only fundamental functionalities come with R. Everything else must be installed from packages. Remind that R is open source; everyone can write code and publish it as a package. You are then able to use this package (and all functions built inside this package) for free. Some packages are installed by default, all others must be installed by running install.packages("name of the package") (do not forget "" around the name of the package!). Once the package is installed, you must load the package and only after it has been loaded you can use all the functions it contains. To load a package, run library(name of the package) (this time "" around the name of the package are optional, but can still be used if you wish). You also have the possibility to install and load packages via the buttons under the Packages tab. For this, click on the button Install under Packages, type the name of the package you want to install and then click on Install. You will see that the code appears in the console. To load the package, find the package you want to load in the Packages window (you can use the search box), then click on the checkbox next to the name of the package. Again, the code is run in the console. See the figures below if needed. Note that you will need to install packages 只有一次,1 但是加载包 每一次 你打开rstudio。此外，请注意，需要Internet连接来安装包，而不需要加载包
• Help: documentation about all functions written for R. To access the help of a function, run help("name of the function") 或者 simply ?name of the function. For example, to see the help about the mean function, run ?mean. You can also press F1 while having your cursor on a function

# 代码的例子

## 计算器

5 * 5
## [1] 25

1 / sqrt(50 * pi) * exp(-(10 - 11)^2 / 50)
## [1] 0.07820854

## 评论

To add comments in your code, use # before the code:

# A comment
# Another comment
1 + 1
## [1] 2

## 存储和打印值

Note that in order to store a value inside an object, using = 或者 <- is equivalent. I however recommend using <- to follow the guidelines of R programming. You can name your objects (A and B in our case) as you like. However, it is recommended to use short and concise names (as you will most likely type them several times) and avoid special characters.

A <- 5
B <- 6

(A <- 5)
## [1] 5

A <- 5
A
## [1] 5

## vectors.

It is also possible to store more than one value inside an object via the function c() (c stands for combine).

A <- c(1 / 2, -1, 0)
A
## [1]  0.5 -1.0  0.0

## 矩阵

Or create a matrix via matrix():

my_mat <- matrix(c(-1, 2, 0, 3), ncol = 2, nrow = 2)
my_mat
##      [,1] [,2]
## [1,]   -1    0
## [2,]    2    3

You can access the help of this function via ?matrix 或者 help("matrix"). Note that inside a function, you can have multiple arguments separated by a comma. Inside matrix(), the first argument is the vector c(-1, 2, 0, 3), the second is ncol = 2 and the third is nrow = 2. For all functions in RStudio, you can specify an argument by its order inside the function or by the name of the argument. If you specify the name of the argument, the order does not matter anymore, so matrix(c(-1, 2, 0, 3), ncol = 2, nrow = 2) is equivalent to matrix(c(-1, 2, 0, 3), nrow = 2, ncol = 2):

my_mat2 <- matrix(c(-1, 2, 0, 3), nrow = 2, ncol = 2)
my_mat2
##      [,1] [,2]
## [1,]   -1    0
## [2,]    2    3
my_mat == my_mat2 # is my_mat equal to my_mat2?
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE

## 生成随机值

my_vec <- rnorm(10, mean = 400, sd = 10)
# Display only the first 5 values:
head(my_vec, 5)
## [1] 401.8291 396.2402 415.8425 398.3830 378.2698
# Display only the last 5 values:
tail(my_vec, 5)
## [1] 387.0264 407.8640 395.6606 392.5291 402.0448

You will have different values than mines due to the fact that they are randomly generated. If you want to make sure to have always the same random values, use set.seed() (with any numeric inside the brackets). For instance, with the following code, you should have the exact same values, no matter where and when you run it:

set.seed(42)
rnorm(3, mean = 10, sd = 2)
## [1] 12.741917  8.870604 10.726257

## 阴谋

plot(my_vec,
type = "l", # "l" stands for line
main = "Plot title",
ylab = "Y-axis label",
xlab = "X-axis label"
)

1. 实际上，您需要重新安装每个新r更新的软件包。但是，如果您在相同的R版本上工作，则需要只安装软件包一次，而是每次打开RStudio时加载它们。↩︎