knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)




Files and folders

At any point in time during a session there is a designated working directory, the default location from which files will be read and where they will be saved. In RStudio it is displayed at the top of the Console window. The default wirking directory (where new sessions start) can be changed in RStudio's Global Options.

Check the working directory with getwd and change it with setwd. The latter also invisibly returns the current working directory, before changing it.

Access paths are accepted and returned as character strings.

The usual access path convention applies, so:

IMPORTANT: Since Windows uses \ instead of /, copying a path from Windows Explorer will cause errors. Always change the separators to / or escape the \ with another \ (the universal escape character).

Autofill is supported and triggered with Tab.

manipulating files

Functions for file management include:




Import and export

Data is most often stored in text files that contain tables. These are usually tab-delimited or comma-separated. The latter commonly have the .csv extension (acronym for comma-separated values) but it is not forced in any way.

Tabular data is imported with the read.table function. It returns a data frame. It has many options that make it quite flexible but usually most of them are unnecessary. There are four wrappers (daughter functions with some preset parameters) that cover the vast majority of cases.

In short, read.delim is used for tab-delimited files and read.csv is used for comma-separated files. They both assume that full stops are used as the decimal separator. read.delim2 and read.csv2 are versions for locales that use commas (the semicolon takes over as the column separator in such .csv files).

Things to keep in mind:

It is highly advisable to carefully read the documentation for read.table at least twice.


Wrtiting tabular data is done with write.table. Key arguments are:

Wrappers for .csv files exist for convenience.




Saving and loading

Data can also be saved in binary files, readable only with R. These are given .Rdata or .rda extensions. A binary file does not hold tabular data in text form but rather a data frame. In fact, it can hold any object(s). Use save to save objects to a binary file and load to bring the contents of a file into the current session.

Another option is the .rds format, which stores one object per file. Files are saved with saveRDS and loaded with readRDS.

Between two files with the same informational content, a binary file will be smaller than a text file, which can be useful for very large data sets. It will also load faster.




Reading files

To read a file that does not contain tabular data use the readLines function. It uses end of line (eol) signs to break up the text into character strings and returns the contents of the file as a character vector.




olobiolo/Rdlazer documentation built on Aug. 6, 2022, 11:37 a.m.