To find the root directory of a directory in R, you can use the normalizePath()
function along with the file.path()
function. The normalizePath()
function resolves a path to an absolute path, and file.path()
concatenates elements of a path.
Here is an example of how to find the root directory of a directory named my_directory
:
main.r299 chars12 lines
In this code, abs_path
gets the absolute path of the current directory using getwd()
and normalizePath()
. path_parts
splits the path into individual elements based on forward slashes and backslashes using strsplit()
. Finally, root_dir
concatenates the root directory and the second element of path_parts
using file.path()
.
Note that the file.path()
function correctly handles path separators for the current operating system. On Unix-like systems, it uses forward slashes, whereas on Windows, it uses backslashes.
gistlibby LogSnag