file.choose() & choose.files() Functions in R (Example)
In this article, I’ll show how to apply the file.choose and choose.files functions in the R programming language.
Table of contents:
Here’s how to do it.
Example 1: Interactively Choose a File Using the file.choose() Function
The following R code shows how to apply the file.choose function to choose a file interactively.
Consider the following R code:
file.choose() # Apply file.choose function
After executing the previous R code, the following window appears:
In this window, we can interactively select a file. Let’s choose the Microsoft Excel file called “my file” in this directory.
After double-clicking on this file, the following output is returned to the RStudio console:
# [1] "C:\\Users\\Joach\\Desktop\\my directory\\my file.xlsx"
As you can see, this output is the file path to our file.
This method can be very useful and efficient when you want to find out the file path of any file on your computer interactively!
Example 2: Interactively Choose a File Using the choose.files() Function
The following R code shows how to apply the choose.files function (note the different ordering of the function name – file.choose vs. choose.files) to choose a file interactively.
choose.files() # Apply choose.files function
After executing this syntax, the following Windows file dialog appears:
If we select our example file in this dialog, the same output as in Example 1 is returned:
# [1] "C:\\Users\\Joach\\Desktop\\my directory\\my file.xlsx"
Note that the choose.files function can also be used to select a list of multiple files as well.
Video & Further Resources
Do you need further explanations on the R programming codes of this tutorial? Then you might watch the following video on my YouTube channel. In the video, I show the R codes of this article in RStudio.
The YouTube video will be added soon.
Furthermore, you could read the related articles on my website. I have published numerous articles already.
- Create Directory & File Path in R
- Extract File & Directory Name from Path
- search & searchpaths Functions in R
- Move Files Between Folders in R
- Important Functions in R
- R Programming Tutorials
In summary: This tutorial has explained how to apply the file.choose and choose.files functions in the R programming language. If you have additional questions, let me know in the comments.
Statistics Globe Newsletter