find the contents of a directory in r

To find the contents of a directory in R, you can use the list.files() function. This function returns a character vector of the file names in a directory.

main.r
# get the list of files in the working directory
list.files()

# get the list of files in a specific directory
list.files('/path/to/directory')
144 chars
6 lines

By default, list.files() returns all files in a directory (including hidden files). You can use the pattern argument to filter for files that match a specific pattern. For example:

main.r
# get the list of all R files in the working directory
list.files(pattern = "\\.R$")
85 chars
3 lines

This will return a vector of file names that end with .R.

Note: When specifying the path of a directory in R, make sure to use forward slashes (/) instead of backslashes (\).

gistlibby LogSnag