hardCode: Facilitate hard coding constants into R

Description Usage Arguments Value Author(s) Examples

View source: R/hardCode.R

Description

Hard coding isn't the best practice, but sometimes it's useful, especially in one-off scripts for analyses. An typical example would be to select a large number of columns in a dataset by their names. This function facilitate hard coding constants into R by printing the code from a vector that would be needed to create that vector.

Usage

1
hardCode(x, vname = "x", vert = TRUE, ...)

Arguments

x

A vector (numeric, character, logical, or complex)

vname

A string indicating the name of the vector that will be "created" in the code

vert

A logical indicating whether the vector should be coded vertically (TRUE) or horizontally (FALSE)

...

Additional arguments to cat

Value

Prints code (via cat) that will create the vector. This code can then be copied into other source code. Returns nothing.

Author(s)

Landon Sego

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# With characters
hardCode(letters[1])
hardCode(letters[1:3], vname = "new")
hardCode(letters[1], vert = FALSE)
hardCode(letters[1:3], vert = FALSE, vname = "new")

# With numbers
hardCode(3:5)
hardCode(3:5, vert = FALSE, vname = "num")

# With logicals
hardCode(TRUE)
hardCode(c(FALSE, TRUE), vert = FALSE)
hardCode(c(TRUE, FALSE, TRUE), vname = "newLogical")

Example output

x <- "a"
new <- c("a",
         "b",
         "c")
x <- "a"
new <- c("a", "b", "c")
x <- c(3,
       4,
       5)
num <- c(3, 4, 5)
x <- TRUE
x <- c(FALSE, TRUE)
newLogical <- c(TRUE,
                FALSE,
                TRUE)

Smisc documentation built on May 2, 2019, 2:46 a.m.