Learn R Programming

Smisc (version 0.3.9.1)

pvar: Prints the name and value of one or more objects

Description

A convenience function for writing the names and values of objects to the session window (and/or to another object). Especially useful to keep track of variables within loops.

Usage

pvar(..., digits = NULL, abbrev = NULL, sep = ";", verbose = TRUE)

Arguments

Objects whose names and values are to be printed, separated by commas. Can also be a simple list.

digits

Number of digits to display for numeric objects. Defaults to NULL, which corresponds to no restriction on the number of digits. This is passed to the digits argument of round.

abbrev

Number of characters to display for character objects. Defaults to NULL, which corresonds to no restriction on the number of characters.

sep

Character string that separates the objects that are printed

verbose

=TRUE writes the value of the object(s) to the session window

Value

Invisibly returns a character string containing the names of the objects and their values

Details

Input objects can be numeric, character, and/or logical. They can also be atomic or vectors. It will accept data frames and matrices without error, but the results won't be easily readable.

Examples

Run this code
# NOT RUN {
x <- 10
y <- 20.728923
z <- "This.long.string"

pvar(x, y, z)
pvar(x, y, z, digits = 2)
pvar(x, y, z, abbrev = 4)
pvar(x, y, z, digits = 2, abbrev = 4)
pvar(x, y, z, sep = ",")

# values can be vectors too
x <- 10:12
y <- c("This","That")
v2 <- pvar(x, y, verbose = FALSE)
v2

# Or a simple list
pvar(list(x = 1:2, y = "this", z = TRUE))

# Can be useful for keeping track of iterations in loops
for (i in 1:2) {
  for (j in letters[1:2]) {
    for (k in c("this","that")) {
      pvar(i, j, k)
    }
  }
}
# }

Run the code above in your browser using DataLab