label: Label Attribute of an Object

View source: R/label.s

labelR Documentation

Label Attribute of an Object


label(x) retrieves the label attribute of x. label(x) <- "a label" stores the label attribute, and also puts the class labelled as the first class of x (for S-Plus this class is not used and methods for handling this class are not defined so the "label" and "units" attributes are lost upon subsetting). The reason for having this class is so that the subscripting method for labelled, [.labelled, can preserve the label attribute in S. Also, the print method for labelled objects prefaces the print with the object's label (and units if there). If the variable is also given a "units" attribute using the units function, subsetting the variable (using [.labelled) will also retain the "units" attribute.

label can optionally append a "units" attribute to the string, and it can optionally return a string or expression (for R's plotmath facility) suitable for plotting. labelPlotmath is a function that also has this function, when the input arguments are the 'label' and 'units' rather than a vector having those attributes. When plotmath mode is used to construct labels, the 'label' or 'units' may contain math expressions but they are typed verbatim if they contain percent signs, blanks, or underscores. labelPlotmath can optionally create the expression as a character string, which is useful in building ggplot commands.

For Surv objects, label first looks to see if there is an overall "label" attribute for the object, then it looks for saved attributes that Surv put in the "inputAttributes" object, looking first at the event variable, then time2, and finally time. You can restrict the looking by specifying type.

labelLatex constructs suitable LaTeX labels a variable or from the label and units arguments, optionally right-justifying units if hfill=TRUE. This is useful when making tables when the variable in question is not a column heading. If x is specified, label and units values are extracted from its attributes instead of from the other arguments.

Label (actually is a function which generates S source code that makes the labels in all the variables in a data frame easy to edit.

llist is like list except that it preserves the names or labels of the component variables in the variables label attribute. This can be useful when looping over variables or using sapply or lapply. By using llist instead of list one can annotate the output with the current variable's name or label. llist also defines a names attribute for the list and pulls the names from the arguments' expressions for non-named arguments.

prList prints a list with element names (without the dollar sign as in default list printing) and if an element of the list is an unclassed list with a name, all of those elements are printed, with titles of the form "primary list name : inner list name". This is especially useful for Rmarkdown html notebooks when a user-written function creates multiple html and graphical outputs to all be printed in a code chunk. Optionally the names can be printed after the object, and the htmlfig option provides more capabilities when making html reports. prList does not work for regular html documents.

putHfig is similar to prList but for a single graphical object that is rendered with a print method, making it easy to specify long captions, and short captions for the table of contents in HTML documents. Table of contents entries are generated with the short caption, which is taken as the long caption if there is none. One can optionally not make a table of contents entry. If argument table=TRUE table captions will be produced instead. Using expcoll, markupSpecs html function expcoll will be used to make tables expand upon clicking an arrow rather than always appear.

putHcap is like putHfig except that it assumes that users render the graphics or table outside of the putHcap call. This allows things to work in ordinary html documents. putHcap does not handle collapsed text.

plotmathTranslate is a simple function that translates certain character strings to character strings that can be used as part of R plotmath expressions. If the input string has a space or percent inside, the string is surrounded by a call to plotmath's paste function. is a utility function that is called by [.data.frame. It is just a copy of data.frame.labelled is another utility function, that adds a class "labelled" to every variable in a data frame that has a "label" attribute but not a "labelled" class.

relevel.labelled is a method for preserving labels with the relevel function.

reLabelled is used to add a 'labelled' class back to variables in data frame that have a 'label' attribute but no 'labelled' class. Useful for changing cleanup.import()'d S-Plus data frames back to general form for R and old versions of S-Plus.


label(x, default=NULL, ...)

## Default S3 method:
label(x, default=NULL, units=plot, plot=FALSE,
      grid=FALSE, html=FALSE, ...)

## S3 method for class 'Surv'
label(x, default=NULL, units=plot, plot=FALSE,
      grid=FALSE, html=FALSE, type=c('any', 'time', 'event'), ...)

## S3 method for class 'data.frame'
label(x, default=NULL, self=FALSE, ...)

label(x, ...) <- value

## Default S3 replacement method:
label(x, ...) <- value

## S3 replacement method for class 'data.frame'
label(x, self=TRUE, ...) <- value

labelPlotmath(label, units=NULL, plotmath=TRUE, html=FALSE, grid=FALSE,

labelLatex(x=NULL, label='', units='', size='smaller[2]',
           hfill=FALSE, bold=FALSE, default='', double=FALSE)

## S3 method for class 'labelled'
print(x, ...)   ## or x - calls print.labelled

Label(object, ...)

## S3 method for class 'data.frame'
Label(object, file='', append=FALSE, ...)

llist(..., labels=TRUE)

prList(x, lcap=NULL, htmlfig=0, after=FALSE)

putHfig(x, ..., scap=NULL, extra=NULL, subsub=TRUE, hr=TRUE,
        table=FALSE, file='', append=FALSE, expcoll=NULL)

putHcap(..., scap=NULL, extra=NULL, subsub=TRUE, hr=TRUE,
        table=FALSE, file='', append=FALSE)



## S3 method for class 'labelled'
relevel(x, ...)





any object (for plotmathTranslate is a character string). For relevel is a factor variable. For prList is a named list. For putHfig is a graphical object for which a print method will render the graphic (e.g., a ggplot2 or plotly object).


lgoical, where to interact with the object or its components


set to TRUE to append the 'units' attribute (if present) to the returned label. The 'units' are surrounded by brackets. For labelPlotmath and labelLatex is a character string containing the units of measurement. When plot is TRUE, units defaults to TRUE.


set to TRUE to return a label suitable for R's plotmath facility (returns an expression instead of a character string) if R is in effect. If units is also TRUE, and if both 'label' and 'units' attributes are present, the 'units' will appear after the label but in smaller type and will not be surrounded by brackets.


if x does not have a 'label' attribute and default (a character string) is specified, the label will be taken as default. For labelLatex the default is the name of the first argument if it is a variable and not a label.


Currently R's lattice and grid functions do not support plotmath expressions for xlab and ylab arguments. When using lattice functions in R, set the argument grid to TRUE so that labelPlotmath can return an ordinary character string instead of an expression.


set to TRUE to use HTML formatting instead of plotmath expressions for constructing labels with units


for Surv objects specifies the type of element for which to restrict the search for a label


a character string containing a variable's label


set to TRUE to have labelMathplot return an expression for plotting using R's plotmath facility. If R is not in effect, an ordinary character string is returned.


set to TRUE to have labelPlotmath return a character string of the form "expression(...)"


LaTeX size for units. Default is two sizes smaller than label, which assumes that the LaTeX relsize package is in use.


set to TRUE to right-justify units in the field. This is useful when multiple labels are being put into rows in a LaTeX tabular environment, and will cause a problem if the label is used in an environment where hfill is not appropriate.


set to TRUE to have labelLatex put the label in bold face.


set to TRUE to represent backslash in LaTeX as four backslashes in place of two. This is needed if, for example, you need to convert the result using as.formula


the label of the object, or "".


a data frame


a list of variables or expressions to be formed into a list. Ignored for print.labelled. For relevel is the level (a single character string) to become the new reference (first) category. For putHfig and putHcap represents one or more character strings that are pasted together, separated by a blank.


the name of a file to which to write S source code. Default is "", meaning standard output. For putHcap, set file to FALSE to return a character vector instead of writing to file.


set to TRUE to append code generated by Label to file file. Also used for putHfig, putHcap.


set to FALSE to make llist ignore the variables' label attribute and use the variables' names.


an optional vector of character strings corresponding to elements in x for prList. These contain long captions that do not appear in the table of contents but which are printed right after the short caption in the body, in the same font.


for prList set to 1 to use HTML markup by running the object names through markupSpecs$html$cap for figure captions. Set htmlfig=2 to also preface the figure caption with "### " so that it will appear in the table of contents.


set to TRUE to have prList put names after the printed object instead of before


a character string specifying the short (or possibly only) caption.


an optional vector of character strings. When present the long caption will be put in the first column of an HTML table and the elements of extra in subsequent columns. This allows extra information to appear in the long caption in a way that is right-justified to the right of the flowing caption text.


set to FALSE to suppress "### " from being placed in front of the short caption. Set it to different character string to use that instead. Set it to "" to ignore short captions entirely. For example to use second-level headings for the table of contents specify subsub="## ".


applies if a caption is present. Specify FALSE to not put a horizontal line before the caption and figure.


set to TRUE to produce table captions instead of figure captions


character string to be visible, with a clickable arrow following to allow initial hiding of a table and its captions. Cannot be used with table=FALSE.


label returns the label attribute of x, if any; otherwise, "". label is used most often for the individual variables in data frames. The function sas.get copies labels over from SAS if they exist.

See Also

sas.get, describe, extractlabs, hlab


age <- c(21,65,43)
y   <- 1:3
label(age) <- "Age in Years"
plot(age, y, xlab=label(age))

data <- data.frame(age=age, y=y)

label(data, self=TRUE) <- "A data frame"
label(data, self=TRUE)

x1 <- 1:10
x2 <- 10:1
label(x2) <- 'Label for x2'
units(x2) <- 'mmHg'
dframe <- data.frame(x1, x2)

labelLatex(x2, hfill=TRUE, bold=TRUE)
labelLatex(label='Velocity', units='m/s')

##In these examples of llist, note that labels are printed after
##variable names, because of print.labelled
a <- 1:3
b <- 4:6
label(b) <- 'B Label'

w <- llist(a, b>5, d=101:103)
sapply(w, function(x){
  hist(as.numeric(x), xlab=label(x))
  # locator(1)   ## wait for mouse click

# Or: for(u in w) {hist(u); title(label(u))}

harrelfe/Hmisc documentation built on May 19, 2024, 4:13 a.m.