The View() function in R is a built-in function that allows users to view the contents of data structures interactively in a spreadsheet-like format. When we use the View() function, it opens a separate window or tab (depending on your R environment) displaying the data in a table format, making it easier to explore and understand the structure and contents of the data.
Features of the View() function
- Displays data in a table format similar to a spreadsheet.
- Allow sorting, filtering, and searching in the data.
- Provides variable types and basic statistics information.
- Provides export data options (e.g., CSV, Excel).
How to Use View()?
1. Creating a Data Frame
First, create a data frame by combining vectors
# Define vectors for each column
ID <- c(1, 2, 3, 4, 5)
Name <- c("Alice", "Bob", "Charlie", "David", "Eva")
Age <- c(25, 30, 22, 35, 28)
Score <- c(85, 92, 78, 95, 89)
# Combine vectors into a data frame
data <- data.frame(ID, Name, Age, Score)
data
Output:
ID Name Age Score
1 1 Alice 25 85
2 2 Bob 30 92
3 3 Charlie 22 78
4 4 David 35 95
5 5 Eva 28 89
2. View the Data Frame
View the created data frame using the View() function to see its contents in a viewer window.
View(data)
Output:

3. Exploring Data
Here we take a Iris dataset and filtering the data based on their Sepal Length which are greater than 5.0 cm.
# Load required packages
library(dplyr)
# Load the Iris dataset
d <- iris
# Filter rows
fil_d <- d %>% filter(Sepal.Length > 5.0)
View(fil_d)
Output:

4. Sort and Search Data
- Sorting: Sort data in ascending or descending order by clicking column headers.
- Search: Search for certain values or patterns in the search box in the viewer
5. Export Data
Utilize options within the viewer window/tab to export data to CSV, Excel, or other formats if needed.
write.csv(filtered_data, "filtered_iris_data.csv", row.names = FALSE)
6. Interacting with Data
Explore and interact with the data dynamically in the viewer window/tab. We can scroll through the data, sort columns, filter rows, or search for specific values interactively.
7. Closing the Viewer
Once finished exploring the data, now close the viewer window/tab manually by clicking on the close button or using the appropriate option provided by the R environment.
Advantages
- Quick and easy data exploration
- Interactive features for insights
- Seamless integration with R environment
Disadvantages
- Limited functionality for complex tasks
- Resource-intensive with large datasets
- Platform-dependent appearance
Related Articles: