swd: Change or list working directories

Description Usage Arguments Value See Also Examples

View source: R/directory.R

Description

Set the working directory to, e.g., a parent directory of the current one, or to a directory visited earlier. Alternatively, list the working directories stored by using swd, or just the current working directory if swd has not been called yet. These are mainly convenience functions for interactive sessions.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  swd(x)

  ## S3 method for class 'NULL'
 swd(x)

  ## S3 method for class 'character'
 swd(x)

  ## S3 method for class 'numeric'
 swd(x)

  listwd(x)

  ## S3 method for class 'NULL'
 listwd(x)

  ## S3 method for class 'numeric'
 listwd(x)

Arguments

x

For swd, a numeric scalar indicating how often to move upwards (i.e., to which parent directory, or character vector containing directory names, or NULL. If x is a negative number, this is used to go to one of the working directories used earlier, using an internally stored directory list. That is, if n is a numeric scalar, the action of swd(n) is not necessarily the inverse of what swd(-n) is doing.

If x is a character vector, its elements passed in turn to setwd.

For listwd, x is an optional numeric scalar indicating how many directories (maximally) to show. The default is 10.

Value

swd yields NULL, returned invisibly. As a side effect, the name of the resulting working directory is printed. This is the only action if x is NULL. The directory stack registers a new directory only via calls to swd itself, not via setwd.

For listwd, a character vector with directory names (current one last), returned invisibly. As a side effect, the list of at most x last directories is printed together with the numeric indexes that would be needed to set them using swd, respectively.

See Also

base::setwd base::getwd

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## listwd()
(d1 <- getwd())
x <- listwd()
stopifnot(x == d1)
swd(1)
x <- listwd()
stopifnot(x == c(d1, dirname(d1)))
swd(-1)
x <- listwd()
stopifnot(x == c(d1, dirname(d1), d1))

## swd()
(d1 <- getwd())
swd(1) # got to immediate parent directory
stopifnot(d1 != getwd(), dirname(d1) == getwd())
swd(d1) # go back, using a name
stopifnot(d1 == getwd())
swd(1) # go upwards again
stopifnot(d1 != getwd(), dirname(d1) == getwd())
swd(-1) # go back, using the position within the visited directories
stopifnot(d1 == getwd())
swd(-2) # go back, using the position again
stopifnot(d1 == getwd())

pkgutils documentation built on May 2, 2019, 5:49 p.m.

Related to swd in pkgutils...