Learn R Programming

Smisc (version 0.3.9.1)

hardCode: Facilitate hard coding constants into 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

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.

Examples

Run this code
# NOT RUN {
# 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")
# }

Run the code above in your browser using DataLab