| 42333 |
ripley |
1 |
% File src/library/utils/man/data.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
| 61596 |
ripley |
3 |
% Copyright 1995-2013 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 2 |
r |
6 |
\name{data}
|
| 56186 |
murdoch |
7 |
\alias{data}
|
| 16280 |
hornik |
8 |
\alias{print.packageIQR}
|
| 2 |
r |
9 |
\title{Data Sets}
|
| 7224 |
hornik |
10 |
\description{
|
| 14590 |
hornik |
11 |
Loads specified data sets, or list the available data sets.
|
| 7224 |
hornik |
12 |
}
|
| 2 |
r |
13 |
\usage{
|
| 53820 |
ripley |
14 |
data(\dots, list = character(), package = NULL, lib.loc = NULL,
|
| 30677 |
ripley |
15 |
verbose = getOption("verbose"), envir = .GlobalEnv)
|
| 4069 |
hornik |
16 |
}
|
| 2 |
r |
17 |
\arguments{
|
| 61596 |
ripley |
18 |
\item{\dots}{literal character strings or names.}
|
| 9439 |
hornik |
19 |
\item{list}{a character vector.}
|
| 30677 |
ripley |
20 |
\item{package}{
|
|
|
21 |
a character vector giving the package(s) to look
|
| 30432 |
ripley |
22 |
in for data sets, or \code{NULL}.
|
|
|
23 |
|
| 30446 |
ripley |
24 |
By default, all packages in the search path are used, then
|
| 30432 |
ripley |
25 |
the \file{data} subdirectory (if present) of the current working
|
|
|
26 |
directory.
|
|
|
27 |
}
|
| 16670 |
hornik |
28 |
\item{lib.loc}{a character vector of directory names of \R libraries,
|
|
|
29 |
or \code{NULL}. The default value of \code{NULL} corresponds to all
|
| 30432 |
ripley |
30 |
libraries currently known.}
|
| 11757 |
hornik |
31 |
\item{verbose}{a logical. If \code{TRUE}, additional diagnostics are
|
|
|
32 |
printed.}
|
| 25508 |
hornik |
33 |
\item{envir}{the \link{environment} where the data should be loaded.}
|
| 9687 |
pd |
34 |
}
|
| 4751 |
hornik |
35 |
\details{
|
| 7081 |
pd |
36 |
Currently, four formats of data files are supported:
|
| 7125 |
hornik |
37 |
|
| 7081 |
pd |
38 |
\enumerate{
|
| 7125 |
hornik |
39 |
\item files ending \file{.R} or \file{.r} are
|
|
|
40 |
\code{\link{source}()}d in, with the \R working directory changed
|
| 7081 |
pd |
41 |
temporarily to the directory containing the respective file.
|
| 39219 |
ripley |
42 |
(\code{data} ensures that the \pkg{utils} package is attached, in
|
|
|
43 |
case it had been run \emph{via} \code{utils::data}.)
|
| 7081 |
pd |
44 |
|
| 23590 |
ripley |
45 |
\item files ending \file{.RData} or \file{.rda} are
|
|
|
46 |
\code{\link{load}()}ed.
|
|
|
47 |
|
| 25080 |
hornik |
48 |
\item files ending \file{.tab}, \file{.txt} or \file{.TXT} are read
|
|
|
49 |
using \code{\link{read.table}(\dots, header = TRUE)}, and hence
|
|
|
50 |
result in a data frame.
|
| 7081 |
pd |
51 |
|
| 23442 |
ripley |
52 |
\item files ending \file{.csv} or \file{.CSV} are read using
|
| 7125 |
hornik |
53 |
\code{\link{read.table}(\dots, header = TRUE, sep = ";")},
|
|
|
54 |
and also result in a data frame.
|
| 7081 |
pd |
55 |
}
|
| 23442 |
ripley |
56 |
If more than one matching file name is found, the first on this list
|
| 50007 |
ripley |
57 |
is used. (Files with extensions \file{.txt}, \file{.tab} or
|
|
|
58 |
\file{.csv} can be compressed, with or without further extension
|
|
|
59 |
\file{.gz}, \file{.bz2} or \file{.xz}.)
|
| 61433 |
ripley |
60 |
|
| 61596 |
ripley |
61 |
The data sets to be loaded can be specified as a set of character
|
|
|
62 |
strings or names, or as the character vector \code{list}, or as both.
|
| 8075 |
ripley |
63 |
|
| 26383 |
hornik |
64 |
For each given data set, the first two types (\file{.R} or \file{.r},
|
|
|
65 |
and \file{.RData} or \file{.rda} files) can create several variables
|
|
|
66 |
in the load environment, which might all be named differently from the
|
| 50007 |
ripley |
67 |
data set. The third and fourth types will always result in the
|
|
|
68 |
creation of a single variable with the same name (without extension)
|
|
|
69 |
as the data set.
|
| 26383 |
hornik |
70 |
|
| 14590 |
hornik |
71 |
If no data sets are specified, \code{data} lists the available data
|
| 25080 |
hornik |
72 |
sets. It looks for a new-style data index in the \file{Meta} or, if
|
|
|
73 |
this is not found, an old-style \file{00Index} file in the \file{data}
|
|
|
74 |
directory of each specified package, and uses these files to prepare a
|
|
|
75 |
listing. If there is a \file{data} area but no index, available data
|
|
|
76 |
files for loading are computed and included in the listing, and a
|
|
|
77 |
warning is given: such packages are incomplete. The information about
|
|
|
78 |
available data sets is returned in an object of class
|
| 30432 |
ripley |
79 |
\code{"packageIQR"}. The structure of this class is experimental.
|
| 30477 |
ripley |
80 |
Where the datasets have a different name from the argument that should
|
|
|
81 |
be used to retrieve them the index will have an entry like
|
|
|
82 |
\code{beaver1 (beavers)} which tells us that dataset \code{beaver1}
|
|
|
83 |
can be retrieved by the call \code{data(beaver)}.
|
| 14590 |
hornik |
84 |
|
| 30432 |
ripley |
85 |
If \code{lib.loc} and \code{package} are both \code{NULL} (the
|
| 30446 |
ripley |
86 |
default), the data sets are searched for in all the currently loaded
|
|
|
87 |
packages then in the \file{data} directory (if any) of the current
|
|
|
88 |
working directory.
|
| 30432 |
ripley |
89 |
|
|
|
90 |
If \code{lib.loc = NULL} but \code{package} is specified as a
|
|
|
91 |
character vector, the specified package(s) are searched for first
|
|
|
92 |
amongst loaded packages and then in the default library/ies
|
|
|
93 |
(see \code{\link{.libPaths}}).
|
|
|
94 |
|
|
|
95 |
If \code{lib.loc} \emph{is} specified (and not \code{NULL}), packages
|
|
|
96 |
are searched for in the specified library/ies, even if they are
|
| 14590 |
hornik |
97 |
already loaded from another library.
|
| 8099 |
ripley |
98 |
|
| 9439 |
hornik |
99 |
To just look in the \file{data} directory of the current working
|
| 30432 |
ripley |
100 |
directory, set \code{package = character(0)} (and \code{lib.loc =
|
|
|
101 |
NULL}, the default).
|
| 3998 |
ripley |
102 |
}
|
| 50070 |
ripley |
103 |
\value{
|
|
|
104 |
A character vector of all data sets specified, or information about
|
|
|
105 |
all available data sets in an object of class \code{"packageIQR"} if
|
|
|
106 |
none were specified.
|
|
|
107 |
}
|
| 61853 |
ripley |
108 |
\section{Good practice}{
|
|
|
109 |
\code{data()} was originally intended to allow users to load datasets
|
|
|
110 |
from packages for use in their examples, and as such it loaded the
|
|
|
111 |
datasets into the workspace \code{\link{.GlobalEnv}}. This avoided
|
|
|
112 |
having large datasets in memory when not in use. That need has been
|
|
|
113 |
almost entirely superseded by lazy-loading of datasets.
|
|
|
114 |
|
|
|
115 |
The ability to specify a dataset by name (without quotes) is a
|
|
|
116 |
convenience: in programming the datasets should be specified by
|
|
|
117 |
character strings (with quotes).
|
|
|
118 |
|
|
|
119 |
Use of \code{data} within a function without an \code{envir} argument
|
|
|
120 |
has the almost always undesirable side-effect of putting an object in
|
|
|
121 |
the user's workspace (and indeed, of replacing any object of that name
|
|
|
122 |
already there). It would almost always be better to put the object in
|
|
|
123 |
the current evaluation environment by \code{data(\dots, envir =
|
|
|
124 |
environment())}. However, two alternatives are usually preferable,
|
|
|
125 |
both described in the \sQuote{Writing R Extensions} manual.
|
|
|
126 |
\itemize{
|
|
|
127 |
\item For sets of data, set up a package to use lazy-loading of data.
|
|
|
128 |
\item For objects which are system data, for example lookup tables
|
|
|
129 |
used in calculations within the function, use a file
|
|
|
130 |
\file{R/sysdata.rda} in the package sources or create the objects by
|
|
|
131 |
\R code at package installation time.
|
|
|
132 |
}
|
| 61926 |
hornik |
133 |
A sometimes important distinction is that the second approach places
|
| 61853 |
ripley |
134 |
objects in the namespace but the first does not. So if it is important
|
|
|
135 |
that the function sees \code{mytable} as an object from the package,
|
|
|
136 |
it is system data and the second approach should be used.
|
|
|
137 |
}
|
| 8099 |
ripley |
138 |
\note{
|
| 9472 |
pd |
139 |
One can take advantage of the search order and the fact that a
|
| 9687 |
pd |
140 |
\file{.R} file will change directory. If raw data are stored in
|
| 9472 |
pd |
141 |
\file{mydata.txt} then one can set up \file{mydata.R} to read
|
| 25080 |
hornik |
142 |
\file{mydata.txt} and pre-process it, e.g., using \code{transform}.
|
| 14590 |
hornik |
143 |
For instance one can convert numeric vectors to factors with the
|
|
|
144 |
appropriate labels. Thus, the \file{.R} file can effectively contain
|
|
|
145 |
a metadata specification for the plaintext formats.
|
| 8099 |
ripley |
146 |
}
|
| 2 |
r |
147 |
\seealso{
|
| 30461 |
ripley |
148 |
\code{\link{help}} for obtaining documentation on data sets,
|
| 25080 |
hornik |
149 |
\code{\link{save}} for \emph{creating} the second (\file{.rda}) kind
|
|
|
150 |
of data, typically the most efficient one.
|
| 61433 |
ripley |
151 |
|
| 50070 |
ripley |
152 |
The \sQuote{Writing R Extensions} for considerations in preparing the
|
|
|
153 |
\file{data} directory of a package.
|
| 2 |
r |
154 |
}
|
|
|
155 |
\examples{
|
| 27541 |
ripley |
156 |
require(utils)
|
| 61168 |
ripley |
157 |
data() # list all available data sets
|
|
|
158 |
try(data(package = "rpart") ) # list the data sets in the rpart package
|
|
|
159 |
data(USArrests, "VADeaths") # load the data sets 'USArrests' and 'VADeaths'
|
| 61596 |
ripley |
160 |
\dontrun{## Alternatively
|
|
|
161 |
ds <- c("USArrests", "VADeaths"); data(list = ds)}
|
| 61168 |
ripley |
162 |
help(USArrests) # give information on data set 'USArrests'
|
| 2 |
r |
163 |
}
|
| 975 |
maechler |
164 |
\keyword{documentation}
|
| 286 |
maechler |
165 |
\keyword{datasets}
|