dir R Function | 3 Example Codes
Basic R Syntax:
The dir R function returns a character vector of file and/or folder names within a directory. The basic syntax for dir in R is illustrated above.
In the following tutorial, I’ll provide you with three examples for the application of dir in the R programming language. Sounds good? Let’s dive right in…
Example 1: Get File Names of Current Directory via getwd() & dir()
path_cwd <- getwd() # Get current working directory path_cwd # Return working directory to RStudio # "D:/.../Statistical Programming"
The getwd command returns the current working directory (i.e. the path of the folder in which we are currently working) as a character. By typing path_cwd <- getwd(), we store the current working directory in the data object path_cwd.
In your case, the path of the current working directory will of cause be different than mine. However, the code can be easily reproduced for your own directory.
Now, we can move on to the application of dir in R:
dir(path_cwd) # Return file and folder names to console #  "~$Upcoming Posts.xlsx" "~$Statistical Programming Previous Posts.xlsx" #  "R Code dir Function.R" "Important Files"
We just have to insert the path of our directory into the dir() command. As you can see, the function returns a character vector that consists of all files and folders stored in this working directory.
In my case, the two xlsx files Upcoming Posts.xlsx and Statistical Programming Previous Posts.xlsx as well as the R code R Code dir Function.R and the folder Important Files are stored into the directory.
Note: You can distinguish files and folders by the ending of the name. My files end with xlsx and R, respectively, but the folder does not have any ending.
Looks good! But what if we want to check for files and folders of another directory? That’s what I’m going to show you now…
Example 2: Apply the R dir Function to Any Directory You Want
The dir R command can also be used to check for file and folder names of other directories than the current working directory. The R code is similar as in Example 1.
First, we have to assign a path to a data object in R…
path_other <- "D:/.../Other Folder" # Assign path of other directory
…and then we can apply the dir function to this path:
dir(path_other) # Return file and folder names to console #  "~$Some random file.xlsx" "Some random folder"
Example 3: Further Arguments for dir in R
The R programming language provides several additional specifications for the dir R function. Even though the previous code is often sufficient, you might want to manually modify the usage of the dir command.
Just write ?dir to the RStudio console in order to get more information from the dir help documentation:
?dir # dir help documentation
Graphic 1: Help Documentation of the dir R Function.
Just specify the available arguments as you need. For instance, the pattern argument can be used to filter for file names with a specific pattern.
Let’s do an example. For the example, I’m using the same code as in Example 1, but this time I’m specifying the argument pattern:
dir(path_cwd, pattern = ".xlsx") # Return only files with specific pattern #  "~$Upcoming Posts.xlsx" "~$Statistical Programming Previous Posts.xlsx"
As you can see, this time the dir function returns only files with the pattern .xlsx.
Video Instructions: How to Set Up Your Working Directory
If you want to use the dir R function, it is crucial that you know how to deal with working directories. If you have problems with that I can recommend the following YouTube video of the MarinStatsLectures channel. The speaker explains in more detail, how to set up a working directory in R properly.