Hamburger Icon
  • Labs icon Lab
  • Data
Labs

Customizing R Environments Hands-on Practice

In this lab, you will learn to efficiently organize data and functions by creating and managing environments in R, a foundational skill crucial for data analysis. You will then explore various types of environments including base, global, and package environments, enhancing their understanding of R's organizational structure and data management. Additionally, the course delves into leveraging libraries and the Comprehensive R Archive Network (CRAN) to access a wide range of functions and datasets, significantly broadening the scope of data analysis capabilities.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 10m
Published
Clock icon Feb 27, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge ourΒ privacy policy.

Table of Contents

  1. Challenge

    Creating and Binding Values to Environments in R

    RStudio Guide

    To get started, click on the 'workspace' folder in the bottom right pane of RStudio. Click on the file entitled "Step 1...". You may want to drag the console pane to be smaller so that you have more room to work. You'll complete each task for Step 1 in that R Markdown file. Remember, you must run the cells with the play button at the top right of each cell for a task before moving onto the next task in the R Markdown file. Continue until you have completed all tasks in this step. Then when you are ready to move onto the next step, you'll come back and click on the file for the next step until you have completed all tasks in all steps of the lab.


    Creating and Binding Values to Environments in R

    To review the concepts covered in this step, please refer to the Presenting Environments and Binding Values in R module of the Customizing R Environments course.

    Understanding how to create an environment and bind values to it in R is important because it allows you to organize your data and functions in a logical and efficient manner. This is a fundamental concept in R programming and is crucial for data analysis.

    Let's put what we've learned into practice! In this step, we'll create a new environment in R and bind some values to it. This will help us understand how R stores and retrieves data in different environments. We'll use the env() function to create a new environment and the <- operator to bind values to it.


    Task 1.1: Create a New Environment

    Load the rlang package, which is already installed. Create a new environment in R and assign it to a variable named my_env.

    πŸ” Hint

    Use the env() function to create a new environment.

    πŸ”‘ Solution
    library('rlang')
    my_env <- env()
    

    Task 1.2: Bind a Value to the Environment

    Assign the value 42 to the variable x in the my_env environment.

    πŸ” Hint

    Use the <- operator to assign a value. Use the $ operator to specify a variable within a specific environment.

    πŸ”‘ Solution
    my_env$x <- 42
    

    Task 1.3: Retrieve the Value from the Environment

    Retrieve the value of x from the my_env environment.

    πŸ” Hint

    Use the $ operator followed by the name of the variable to retrieve its value from the environment.

    πŸ”‘ Solution
    my_env$x
    
  2. Challenge

    Identifying Types of Environments in R

    Identifying Types of Environments in R

    To review the concepts covered in this step, please refer to the Identifying Types of Environments in R module of the Customizing R Environments course.

    Knowing the different types of environments in R is important because it helps you understand how R organizes data and functions. This knowledge is crucial when you're working with packages and libraries in R.

    Time to dive deeper into the world of R environments! In this step, we'll identify the different types of environments in R, including the base, global, and package environments. We'll use the find() function to find out what environment a function or value is in, and the search() function to list all the environments currently in use.


    Task 2.1: Identifying the Environment of a Function

    Find out what environment the mean function is in. Use the find() function, which is an updated function similar to the where() function from pryr.

    πŸ” Hint

    To determine the environment of a function, you can use the find() function and provide the name of the function as an argument. The name of the function should be a character variable (i.e., it should be surrounded by quotes).

    πŸ”‘ Solution
    find('mean')
    

    Task 2.2: Identifying the Environment of a Value

    Find out what environment the variable pi is in using the find() function.

    πŸ” Hint

    To find the environment of a variable, you can use the find() function and provide the name of the variable as an argument.

    πŸ”‘ Solution
    find('pi')
    

    Task 2.3: Listing All Environments

    List all the environments currently in use using the search() function.

    πŸ” Hint

    To list all the environments currently in use, you can simply call the search() function without any arguments.

    πŸ”‘ Solution
    search()
    

    Task 2.4: Identifying the Package Environment

    Load the ggplot2 package and the devtools package. This will give you access to several new functions.
    Identify which package the function aes comes from.

    πŸ” Hint

    Load the packages using the library() function, using the package name as the argument. Then use the find() function to identify the environment of the aes function.

    πŸ”‘ Solution
    library('ggplot2')
    library('devtools')
    find('aes')
    
  3. Challenge

    Working with Libraries and CRAN in R

    Working with Libraries and CRAN in R

    To review the concepts covered in this step, please refer to the Listing Environments and Presenting Libraries from a CRAN in R module of the Customizing R Environments course.

    Understanding how to use libraries and the Comprehensive R Archive Network (CRAN) in R is important because it enables you to access a vast array of pre-built functions and datasets that can greatly enhance your data analysis capabilities.

    Let's explore the power of libraries and CRAN in R! In this step, we'll learn how to use the library() function to load a library, and the getCRANmirrors() function to list available CRANs. We'll also use the library() function to find documentation for a package. This will give us a good understanding of how to leverage external resources in R.


    Task 3.1: Loading a Library in R

    Load the ggplot2 library.

    πŸ” Hint

    To load a library in R, you should use the library() function and specify the name of the library inside single or double quotes as an argument.

    πŸ”‘ Solution
    library('ggplot2')
    

    Task 3.2: Listing Available CRANs in R

    List all available CRAN mirrors.

    πŸ” Hint

    To list available CRAN mirrors, simply call the getCRANmirrors() function without any arguments.

    πŸ”‘ Solution
    getCRANmirrors()
    

    Task 3.3: Finding Documentation for a Package in R

    Use the help argument of the library() function to find documentation for the ggplot2 package.

    πŸ” Hint

    Call the library() function. The first argument should be the named argument help, and the value of that argument should be the package name, in this case 'ggplot2'.

    πŸ”‘ Solution
    library(help = 'ggplot2')
    

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.