ls(): Lists of variables saved in the workspace

As we define objects in the console, we are actually changing the workspace. You can see all the variables saved in your workspace by typing:

ls()
#> [1] "a"        "b"        "c"        "img_path" "murders"

In RStudio, the Environment tab shows the values:

1-5_ls.png

We should see a, b and c. If you try to recover the value of a variable that is not in your workspace, you receive an error. For example, if you type x you will receive the following message: Error: object 'x' not found.

If you want to save memory spaces in workspace, you can use rm() function to remove variable in your workspace.

Instruction

# Assign sample variables a <- "Happy R Coding" b <- iris$Sepal.Length name <- c("Jongjin", "Kim") # Look up the variables in the workspace. ls() # Create new variable called x and assign log(8, base=2). x <- # Look up the variables in the workspace after assigning x. print("workspace after creating x") ls() # Remove a variable called 'name' in a workspace rm(_____) # Look up the variables in the workspace after assigning x. print("workspace after removing name") ls() # Assign sample variables a <- "Happy R Coding" b <- iris$Sepal.Length name <- c("Jongjin", "Kim") # Look up the variables in the workspace. ls() # Create new variable called x and assign log(8, base=2). x <- log(8, base=2) # Look up the variables in the workspace after assigning x. print("workspace after creating x") ls() # Remove variable name in a workspace rm(name) # Look up the variables in the workspace after assigning x. print("workspace after removing name") ls() test_object("x", undefined_msg = "Make sure to define a variable `x`.", incorrect_msg = "Make sure that you assign the correct value to `x`.") test_output_contains("ls()", incorrect_msg = "Make sure that you type the commamnd ls()") test_output_contains("rm(name)", incorrect_msg = "Make sure that you remove name from your workspace.") success_msg("Great! Head over to the next exercise.")

Previous: 1-4 | head(): Look up the first entries in variables

Next: 1-6 | help(): Help document for pre-built functions

Back to Main