Home » How to Use file.choose() in R (With Example)

How to Use file.choose() in R (With Example)

by Tutor Aspire

You can use the file.choose() function in R to bring up a file explorer window that allows you to interactively choose a file path to work with.

To use this function, simply type the following into your R console:

file.choose()

The following example shows how to use this function in practice.

Example: How to Use file.choose() in R

Suppose we have some file called df1.csv located in a folder called my_data_files that we’d like to import into our R environment:

However, suppose we don’t know the exact file path of this CSV file.

To quickly find it, we can type file.choose() into our R console:

A file explorer window will appear where we can then navigate to the folder that contains this file:

Note: If you don’t see the file explorer window, check to see if it opened behind RStudio.

When you double click on the file you’d like, the file path will automatically appear in your R console:

[1] "C:\Users\bob\Documents\my_data_files\df1.csv"

You can then use this path to import the file into R:

#import df1.csv file
df csv("C:\Users\bob\Documents\my_data_files\df1.csv")

#view result
df

  points assists
1      4       3
2      5       2
3      5       4
4      6       4
5      8       6
6      9       3

We’re able to successfully import the CSV file into a data frame in R since we used the correct file path.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use list.files() in R
How to Import CSV Files into R
How to Import Excel Files into R

You may also like