:R provides a handful of functions for interacting with files in your working directory. This can save you the trouble of having to open a separate file browser to list or delete files, or having to open a separate text editor to read text files. Some of these functions appear below.
:::rsplus
> getwd()
[[1]] "/myflder/myRproject"
> setwd("anotherfolder/myRproject")`</code>`
Note that setwd() does not give confirmation, though it will complain if it fails. You can either specific absolute path names, or choose a path relative to your current working directory.
#### List files and folders contained in the current working directory
`<code rsplus>`
> list.files()
[[1]] "data.txt"
[[2]] "myscript.r"
[[3]] "myscript2.r"
[[4]] "Rhelpmanual.pdf"
[[5]] "somerandomfolder"`</code>`
Note for Linux users: At the R command line in a terminal window, [[Tab]] can be used for filename completion. Using the example above, typing "d" followed by [[Tab]] will result in "data.txt" appearing on the command line. This can spare you the trouble of typing out long file names within functions (e.g., source(), read.table(), write.csv(), etc.). Note also that typing [[Tab]][Tab] at the R command line will list all files in your current working directory, similar to the list.files() function.
#### Get basic information about a file (e.g. "data.txt") in the current working directory
`<code rsplus>`
> file.info("data.txt")
size isdir mode mtime ctime atime uid gid uname grname
data.txt 2418 FALSE 777 2005-10-09 21:09:11 2005-10-09 21:09:11 2005-11-27 22:57:30 1000 0 regetz root`</code>`
#### Open and view a text file (e.g. "mynotes.txt")
`<code rsplus>`
> file.show("mynotes.txt")`</code>`
The specific behavior is OS-dependent; e.g. in Linux the file may be displayed in the current window using 'less', while in WinXP the file may be displayed in Notepad.
#### Open and edit a text file (e.g. "data.csv")
`<code rsplus>`
# Without options, the default editor is used (this varies by system)
> file.edit("data.csv")
# Use the editor parameter to specify a particular editor:
> file.edit("data.csv", editor="notepad")
> file.edit("data.csv", editor="vi")`</code>`
#### Delete a file in the working directory (use this carefully!)
`<code rsplus>`
> unlink("filename") #File is deleted silently
> file.remove("filename") #As above, but success is confirmed
[[1]] TRUE`</code>`
#### Send commands directly to the operating system
`<code rsplus>`
# Linux users may prefer to issue shell commands directly, rather than
# using the above wrapper functions. For example:
> system("ls")
data.txt
myscript.r
myscript2.r
Rhelpmanual.pdf
somerandomfolder
> system("streamtuner") #Take a break and listen to some tunes!`</code>`
#### Construct a path from component directories
`<code rsplus>`
# specify the separator, e.g. "/":
> file.path("c:","foo","bar","somefolder", fsep="/")
# to use the default separator:
> file.path("c:","foo","bar","somefolder")
# Note that the default separator is stored in .Platform$file.sep`</code>`