Sys.glob Function in R (Example) | Wildcard Expansion & Globbing on File Paths
In this R tutorial you’ll learn how to conduct a wildcard expansion (or globbing) on file paths using the Sys.glob function.
The article will consist of one example for the application of the Sys.glob function. To be more precise, the article is structured as follows:
Let’s get started!
Example: Perform Wildcard Expansion on File Paths Using Sys.glob() Function
The code below shows how to conduct globbing on file directory paths using the Sys.glob and the file.path functions.
For this, I have created a directory and several subdirectories containing different TXT files.
We can extract the paths to all of these TXT files as shown below:
Sys.glob(file.path("C:/Users/Joach/Desktop/my directory", "*", "*.txt")) # Apply Sys.glob function # [1] "C:/Users/Joach/Desktop/my directory/folder 1/my file 1.txt" # [2] "C:/Users/Joach/Desktop/my directory/folder 1/my file 2.txt" # [3] "C:/Users/Joach/Desktop/my directory/folder 2/my file 3.txt" # [4] "C:/Users/Joach/Desktop/my directory/folder 3/my file 4.txt"
The previous R code has returned all paths to my TXT files based on a wildcard pattern in our file paths.
Video, Further Resources & Summary
I have recently published a video on the Statistics Globe YouTube channel, which illustrates the R programming syntax of this article. You can find the video below.
The YouTube video will be added soon.
Furthermore, you may want to have a look at the other articles on my homepage. I have published numerous other tutorials already.
- Match Wildcard Pattern and Character String in R
- Locate & Extract Regular Expression Match
- System Calls & Commands
- Important Functions in R
- All R Programming Tutorials
To summarize: In this article, I have demonstrated how to apply the Sys.glob function in R programming. Don’t hesitate to tell me about it in the comments section, in case you have any additional questions.
Statistics Globe Newsletter