The View() function in R creates a virtual data frame subset of an existing data frame. This allows you to work with a specific subset of the data without creating a new data frame and copying the relevant data into it.
The View() function invokes a spreadsheet-style data viewer on a matrix-like R object. It can help you see the contents and structure of an object more clearly.
ObjectName: An R object coerced to a data frame with non-zero numbers of rows and columns.
title: It is a title for a viewer window. Defaults to the ObjectName prefixed by Data.
It returns an Invisible NULL. The functions put up a window and return immediately: the window can be closed via its controls or menus.
Example 1: How to use View() function
To demonstrate the View() function, you must install RStudio on your machine.
Open the RStudio. Create an R file and add the following code.
df <- structure(list(year = c(2016, 2017, 2018, 2019), length_days = c(365.32, 366.41, 366.53, 364.95)), .Names = c("year", "days"), class = "data.frame", row.names = c(NA, -4L)) View(df)
In this example, we create a data frame using the structure() function.
To see the output, run the above code inside the RStudio, and it will give you the following output.
Viewing the mode of an object
To find what type of data is contained within the object, use the mode() function. The mode of an object can be viewed using the mode function.
df <- structure(list(year = c(2016, 2017, 2018, 2019), length_days = c(365.32, 366.41, 367.53, 368.95)), .Names = c("year", "days"), class = "data.frame", row.names = c(NA, -4L)) mode(df)
How to Sort Data Using the View() Function
To sort data using the View() function in R, you can click on one of the columns in the data viewer, and it will automatically sort the rows by that column. For example, if you have a data frame named df with columns x and y, you can use View(df) and then click x to sort by x.
View() function in R is not working
If you can’t view the data frame using the View() function, you often think that the view function is not working correctly in R, But in reality, there could be a version problem you are using because it is not supported.
If you are using, for example, R Version 3.1.2 with RStudio Version 0.98.1091, then the View() function from the “utils” package is not supported. So to solve this, you should upgrade your RStudio.
So, often you can solve this type of error by updating your packages related to R.
View() function in RStudio
RStudio includes a data viewer that allows you to look inside data frames and other rectangular data structures.
You can invoke the viewer in a console by calling the View() function on the data frame you want to look at. For instance, to view the built-in mtcars dataset, run these commands.
You can also sort the data of the viewer. You can sort by any column by just clicking on the column. Then, click on a column that’s already sorted to reverse the sort direction.
We have sorted the column mpg in descending order.
You can also perform searching, filtering, saving filters, and auto-refreshing. For more information, please check out the data viewer guide for RStudio.
The View() function in R invokes a spreadsheet-style data viewer on a matrix-like object. For example, if you have a data frame named df, you can use the View(df) function to see it in a tabular format.
To view all the contents of a defined object, use the View() function. Behind the scenes, the R calls utils::View() on the input and returns it invisibly.
Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.