R Setup
The R language's development environment itself includes a graphical development environment, which is different from many other engineering languages. Therefore, the development environment is best installed on an operating system designed for desktop personal computers (such as Windows, macOS, or Ubuntu Desktop, etc.).
First, we need to download the R language environment installation package:
### Windows
* Official: [https://cloud.r-project.org/bin/windows/base/](https://cloud.r-project.org/bin/windows/base/)
* USTC Mirror: [https://mirrors.ustc.edu.cn/CRAN/bin/windows/base/](https://mirrors.ustc.edu.cn/CRAN/bin/windows/base/)
* TUNA Mirror: [https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/base/](https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/base/)
### Linux
* Official: [https://cloud.r-project.org/bin/linux/](https://cloud.r-project.org/bin/linux/)
* USTC Mirror: [https://mirrors.ustc.edu.cn/CRAN/bin/linux/](https://mirrors.ustc.edu.cn/CRAN/bin/linux/)
* TUNA Mirror: [https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/linux/](https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/linux/)
### macOS
* Official: [https://cloud.r-project.org/bin/macosx/](https://cloud.r-project.org/bin/macosx/)
* USTC Mirror: [https://mirrors.ustc.edu.cn/CRAN/bin/macosx/](https://mirrors.ustc.edu.cn/CRAN/bin/macosx/)
* TUNA Mirror: [https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/](https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/)
The above versions may be outdated. If you need the latest version, you can visit:
* Tsinghua University Mirror: [https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/](https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/)
* Official: [https://cloud.r-project.org/bin/](https://cloud.r-project.org/bin/)
* * *
## Windows Operating System
Windows installation is simple. After downloading the installation package, double-click the downloaded installation package to start the installation wizard:
!(#)
!(#)
!(#)
**Note:** The operating system used here is 64-bit, but there are still a few computers using 32-bit operating systems. If your operating system is 32-bit, please select the "32-bit User Installation" option at this step.
!(#)
!(#)
When we enter the following code in the interactive command window:
print("Hello, world")
The output result is:
"Hello, world"
## Linux
### Ubuntu Installation
Execute the following commands to install the R language runtime environment:
# sudo apt update
# sudo apt -y upgrade
# sudo apt -y install r-base
After successful installation, executing the R command will enter the interactive programming window:
!(#)
### Centos Installation
# sudo yum install R
Enter the following command to view the installed version:
# R --version
You can exit the interactive command by entering q():
> q()
Save workspace image? [y/n/c]: y
## macOS Installation
Installing the R language environment on macOS is similar to Windows. Download the pkg installation package, double-click the installation package to open it, and then follow the installation wizard:
!(#)
!(#)
!(#)
!(#)
After successful installation, executing the R command will enter the interactive programming window:
!(#)
You can exit the interactive command by entering q():
> q()
Save workspace image? [y/n/c]: y
!(#)
### Script Execution
In R language, you can use the Rscript command in the command line to execute R script files.
The Rscript command allows you to run R scripts directly from the command line without opening the R console.
To use the Rscript command to execute an R script file, you can follow these steps:
Create an R script file containing the R code you want to execute. For example, save the following code as **script.R** file:
## script.R file code:
# script.R
x <-1:10
y <- x^2
print(y)
Open the terminal or command
YouTip