About

File (System, Transfer, Storage) in R

See also: system.file {base} to finds the full file names of files in packages etc.

Management

Special Directory

R Home

R.home(component = "home")
[1] "C:\\Users\\gerard\\Documents"

User Home

path.expand("~")
[1] "C:\\Users\\gerard\\Documents"

Working Directory

  • To get the working directory, use the function getwd
  • To set the working directory, use the function setwd

Example:

> getwd()
[1] "C:/Users/gerard/Documents"
> setwd("C:/Users/gerard/Desktop")

Temporary

tempDir <- tempdir()
tempFile <- paste0(tempDir , "/temp.txt")
temp_csv <- tempfile(fileext = ".csv")

Create, open and close a file

File is a function to create, open and close a file.

file(
     description = "",
     open = "", 
     blocking = TRUE,
     encoding = getOption("encoding"), 
     raw = FALSE
     )

where:

  • description is the path to the file (If it does not contain an absolute path, the file name is relative to the current working directory)
  • open indicates the open mode.

List files in a directory

dir()

Time

  • File Time: Sys.setFileTime()

Separator

See R Admin - OS (Platform, Sys, )

.Platform$file.sep
[1] "/"