File operations‎ > ‎

File commands






# get working directory
getwd()
[1] "/home/username"

# show content of current folder
dir()
[1] "Desktop"  "Documents"  "Downloads"  "R"

# cd  change into a directory
setwd("/path/to/new/directory")


# Path in Windows
a) use double backslash (to avoid string interpretation by R)
filepath=('C:\\path\\to\\file.tsv')
b) using slash instead of backslash, also for Windows path in R
filepath=('C:/path/to/file.tsv')


# platform specific file separator: "/" Unix or "\" Windows
.Platform$file.sep
path = paste(path, .Platform$file.sep, filename,  sep=''")

# create path of directory and file name
path = paste(filedir, filename, sep = "/")
path = paste(filedir, filename, sep=.Platform$file.sep)
# or, better using file.path:
file.path(getwd(), 'myfile.txt')


# path completion
'~/Projects/  ...  <Tab>

# create dir if doesn't exist
mydir = 'testDir'
if !file.exists(mydir) dir.create(mydir)