getwd, setwd

R is always pointed at a directory on your computer. You can find out which directory by running the getwd (get working directory) function; this function has no arguments. To change your working directory, use setwd and specify the path to the desired folder.
getwd()
setwd(dir)
  • dir – Specify a working directory. This may be from the root directory (starting with / on a Mac), it may include a double-dot (..) to move locally up a folder from the current directory, and it may include a path from the current directory.

Continue reading

package.skeleton

package.skeleton(name, list, environment = .GlobalEnv, path=".")
Initialize the files for an R package. First load in all the objects to be included in the package into the current R session, then run the package.skeleton command. See the Building Packages tab on this blog for videos about building R packages.
  • name – A character string for the name of the package.
  • list – If only some objects in the current session should be included in the package, list them here in a character vector.
  • environment – The environment where the objects are looked for. (Most users should leave this argument alone.)
  • path – By default, the package will be saved in the current working directory. Change the path argument to specify a different place to save the package files.

Continue reading

TRUE and FALSE, not T and F

TRUE != T, FALSE != F
It is common to see code that uses T for TRUE and F for FALSE, but this can lead to unexpected errors. By default, R assumes T and F are TRUE and FALSE. However, these variables are sometimes overwritten, whereas TRUE and FALSE cannot be overwritten. For example, T is occasionally used for a time variable by users; if the first element of T is zero, then there can be serious consequences.
This issue is so severely overlooked that it is common to read a textbook highlighting the use of R and see inappropriate use of T and F.

Continue reading

pdf, png, dev.off

Initialize a plot that will be written directly to a file using pdf or png. Then create a plot using the usual functions in R. Finally, close the file using the dev.off function. There are also bmp, tiff, and jpeg functions, though the jpeg function has proven less stable than the others.
pdf(filename, width=7, height=7)
  • filename – A character string for the file name, ending in .pdf.
  • width – Width of the image, in inches.
  • width – Height of the image, in inches.
png(filename, width=480, height=480)
  • filename – A character string for the file name, ending in .png.
  • width – Width of the image, in inches.
  • width – Height of the image, in inches.
dev.off()

Continue reading

class, typeof

Determine the class of an object or its "internal" type. The former will be useful to most R users while the latter only to a smaller subset. Note that the class function is much more powerful than it first appears. You can use it to create new S3 classes in R. (This post isn’t complete on this topic, but I’ll provide more information in the coming weeks.)
class(x)
  • x – Any R object.
typeof(x)
  • x – Any R object.

Continue reading