IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Getting Started with R

    sarika发表于 2024-10-18 09:58:35
    love 0
    [This article was first published on R feed, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

    R is a versatile, high-level programming language widely supported across all major operating systems.

    You can run R on your using the following two methods:

    • Run R online
    • Install R on your computer

    In this tutorial, you will learn both methods.

    Run R Online

    You need an R interpreter installed to run the R code. However, you can use our free online R editor to start immediately.

    Online R Compiler
    Online R Compiler

    This online editor enables you to run R code directly in your browser—no installation is required.


    Install R on Your Computer

    For those who prefer to install Python on your computer, this guide will walk you through the installation process on Windows, macOS, or Linux (Ubuntu).

    Windows
    MacOS
    Linux

    To install R on your Windows, just follow these steps:

    1. Install VS Code
    2. Download the R Installer File
    3. Run the Installer
    4. Install R
    5. Add R to the PATH Environment Variable Manually
    6. Verify the Installation

    Here is a detailed explanation of each of the steps:

    Step 1: Install VS Code

    Go to the VS Code Official website and download the Windows installer. Once the download is complete, run the installer and follow the installation process.

    Click Finish to complete the installation process

    Step 2: Download the R Installer File

    Go to the official official R Project website and download the latest version of R for Windows.

    R Download Page
    R Download Page

    Step 3: Run the Installer

    Go to your download folder and run the installer you just downloaded. Depending on your security settings, you might be prompted to allow access.

    Simply allow it and proceed.

    Step 4: Install R

    Once you have run the installer, you will come across this screen.

    Install R on Windows
    Install R on Windows

    Choose your preferred language.(here English)

    You will be asked to choose the installation path. By default, it will install in C:\Program Files\R\. You can leave this as it is and proceed with the installation.

    Select Destination Location for R
    Select Destination Location for R

    The installer will then prompt you to choose components and where to place shortcuts. Click Next for each step to proceed with the default options for a standard setup.

    Select Components for Installation
    Select Components for Installation

    Finally, click Install to start the installation. The process may take a few minutes to complete.

    R Installation Process
    R Installation Process

    After the installation is complete, you will see this screen:

    R Installation
    R Installation

    Click Finish to exit the installer.

    Step 5: Add R to the PATH Environment Variable Manually

    You must manually ensure your system’s PATH environment variable is set correctly. Follow these steps to add the PATH environment variable.

    1. Copy the path where R is installed to the bin folder. (usually, it is in C:\Program Files\R\R-x.x.x\bin)

    2. Right-click on This PC and select Properties.

    3. Click on Advanced System Settings and then Environment Variables.

    Environment Variables
    Environment Variables

    4. Under System Variables, find and select Path, then click Edit.

    Edit Path Under System Variables
    Edit Path Under System Variables

    5. Click New and paste the path.

    Edit Environment Variable
    Edit Environment Variable

    6. Click OK to close all dialogs and apply the changes.

    Step 6: Verify the Installation

    Once the installation is complete, you can verify that R is installed successfully by opening a command prompt (cmd) and typing the following command:

    R --version
    R Installation Verification for Windows
    R Installation Verification for Windows

    Now, you are all set to run R programs on your device.

    To install R on your Mac, just follow these steps:

    1. Install VS Code
    2. Install Homebrew (if not installed)
    3. Install R
    4. Verify your installation

    Here is a detailed explanation of each of the steps:

    Step 1: Install VS Code

    Go to the VS Code official website and download the zipped file. Once the download is complete, open the zipped file.

    In Finder, open a new window and navigate to the Applications folder. To install the VS Code application from the zip file, drag it into the Applications folder.

    You can now launch VS Code directly from the Applications folder.

    Step 2: Install Homebrew

    Homebrew is a package manager that simplifies the installation of software on macOS. Open the Terminal app and type the following command to install Homebrew (if it’s not already installed):

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    Homebrew Installation on Mac
    Homebrew Installation on Mac

    When this installation is complete, we will use homebrew to install R.

    Step 3: Install R

    Now that Homebrew is installed, you can easily install Rust. In the Terminal, enter the following command:

    brew install r
    R Installer for Mac
    R Installer for Mac

    This command will download and install the latest version of R on your macOS system.

    Step 5: Verify your installation

    Once the installation is complete, you must verify that R is correctly installed. In the Terminal, type the following command:

    R
    R Installation Verification for Mac
    R Installation Verification for Mac

    Note: The version number might differ from the one above, depending on your installed version.

    Linux has various distributions, and the installation process differs slightly from one to the next. For now, we will focus on the Ubuntu distribution.

    Most Linux distributions come pre-installed with Python. However, if you need to install or upgrade, follow these steps:

    1. Install VS Code
    2. Add CRAN Package
    3. Add CRAN gpg Key
    4. Install r-base
    5. Verify Installation

    Here is a detailed explanation of each of the steps:

    Step 1: Install VS Code

    Open the Terminal and type

    sudo apt update

    This command updates your package lists to ensure you get the latest versions of your software.

    Proceed to install VS Code with

    sudo snap install code --classic

    Step 2: Add CRAN Package

    We first need to add the CRAN package to our system to install R. Use the following command to do so,

    echo "deb https://cloud.r-project.org/bin/linux/ubuntu noble-cran40/" | sudo tee /etc/apt/sources.list.d/r.list
    Add CRAN Package in Ubuntu
    Add CRAN Package in Ubuntu

    Step 3: Add the CRAN gpg Key

    After the CRAN package is added, we need to add the CRAN gpg key; use the following command to do so,

    wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/CRAN_ubuntu_key.asc
    Add CRAN GPG Key on Ubuntu
    Add CRAN GPG Key on Ubuntu

    Step 4: Install r-base

    Now to install the complete R system, use the following command.

    sudo apt install y r-base
    Install R on Ubuntu
    Install R on Ubuntu

    Step 5: Verify Installation

    To verify that R has been installed successfully, use the following command,

    R
    Verify R Installation on Ubuntu
    Verify R Installation on Ubuntu

    Now, you can run the R program on your device.


    Run Your First R Program

    First, open VS Code, click on the File in the top menu, and then select New File.

    Create a New File in VS Code
    Create a New File in VS Code

    Then, save this file with a .r extension by clicking on File again, then Save As, and type your filename ending in .r. (Here, we are saving it as hello.r).

    Before coding, ensure the R extension is installed in VS Code. Open VS Code and click on Extensions on the left sidebar. Then, search for the R extension and click on install.

    Install R Extension on VS Code
    Install R Extension on VS Code

    Now, write the following code into your file:

    message <-"Hello, World"
    print(message)

    Then click the run button on the top right side of your screen.

    Run R Program
    Run R Program

    You should see Hello, World printed on the terminal.

    Now that you have set everything up to run R programs on your computer, you'll learn how R's basic program works in the next tutorial.

    To leave a comment for the author, please follow the link and comment on their blog: R feed.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
    Continue reading: Getting Started with R


沪ICP备19023445号-2号
友情链接