| 42333 |
ripley |
1 |
% File src/library/utils/man/data.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 81513 |
ripley |
3 |
% Copyright 1995-2022 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,
|
| 76115 |
ripley |
15 |
verbose = getOption("verbose"), envir = .GlobalEnv,
|
| 76116 |
ripley |
16 |
overwrite = TRUE)
|
| 4069 |
hornik |
17 |
}
|
| 2 |
r |
18 |
\arguments{
|
| 61596 |
ripley |
19 |
\item{\dots}{literal character strings or names.}
|
| 9439 |
hornik |
20 |
\item{list}{a character vector.}
|
| 30677 |
ripley |
21 |
\item{package}{
|
|
|
22 |
a character vector giving the package(s) to look
|
| 30432 |
ripley |
23 |
in for data sets, or \code{NULL}.
|
|
|
24 |
|
| 30446 |
ripley |
25 |
By default, all packages in the search path are used, then
|
| 30432 |
ripley |
26 |
the \file{data} subdirectory (if present) of the current working
|
|
|
27 |
directory.
|
|
|
28 |
}
|
| 16670 |
hornik |
29 |
\item{lib.loc}{a character vector of directory names of \R libraries,
|
|
|
30 |
or \code{NULL}. The default value of \code{NULL} corresponds to all
|
| 30432 |
ripley |
31 |
libraries currently known.}
|
| 11757 |
hornik |
32 |
\item{verbose}{a logical. If \code{TRUE}, additional diagnostics are
|
|
|
33 |
printed.}
|
| 25508 |
hornik |
34 |
\item{envir}{the \link{environment} where the data should be loaded.}
|
| 76115 |
ripley |
35 |
\item{overwrite}{logical: should existing objects of the same name in
|
|
|
36 |
\env{envir} be replaced?}
|
| 9687 |
pd |
37 |
}
|
| 4751 |
hornik |
38 |
\details{
|
| 7081 |
pd |
39 |
Currently, four formats of data files are supported:
|
| 7125 |
hornik |
40 |
|
| 7081 |
pd |
41 |
\enumerate{
|
| 7125 |
hornik |
42 |
\item files ending \file{.R} or \file{.r} are
|
|
|
43 |
\code{\link{source}()}d in, with the \R working directory changed
|
| 7081 |
pd |
44 |
temporarily to the directory containing the respective file.
|
| 39219 |
ripley |
45 |
(\code{data} ensures that the \pkg{utils} package is attached, in
|
|
|
46 |
case it had been run \emph{via} \code{utils::data}.)
|
| 7081 |
pd |
47 |
|
| 86671 |
hornik |
48 |
\item files ending \file{.RData} or \file{.rdata} or \file{.rda} are
|
| 23590 |
ripley |
49 |
\code{\link{load}()}ed.
|
|
|
50 |
|
| 25080 |
hornik |
51 |
\item files ending \file{.tab}, \file{.txt} or \file{.TXT} are read
|
| 70189 |
pd |
52 |
using \code{\link{read.table}(\dots, header = TRUE, as.is=FALSE)},
|
|
|
53 |
and hence
|
| 25080 |
hornik |
54 |
result in a data frame.
|
| 7081 |
pd |
55 |
|
| 23442 |
ripley |
56 |
\item files ending \file{.csv} or \file{.CSV} are read using
|
| 70189 |
pd |
57 |
\code{\link{read.table}(\dots, header = TRUE, sep = ";", as.is=FALSE)},
|
| 7125 |
hornik |
58 |
and also result in a data frame.
|
| 7081 |
pd |
59 |
}
|
| 23442 |
ripley |
60 |
If more than one matching file name is found, the first on this list
|
| 50007 |
ripley |
61 |
is used. (Files with extensions \file{.txt}, \file{.tab} or
|
|
|
62 |
\file{.csv} can be compressed, with or without further extension
|
|
|
63 |
\file{.gz}, \file{.bz2} or \file{.xz}.)
|
| 61433 |
ripley |
64 |
|
| 61596 |
ripley |
65 |
The data sets to be loaded can be specified as a set of character
|
|
|
66 |
strings or names, or as the character vector \code{list}, or as both.
|
| 8075 |
ripley |
67 |
|
| 26383 |
hornik |
68 |
For each given data set, the first two types (\file{.R} or \file{.r},
|
|
|
69 |
and \file{.RData} or \file{.rda} files) can create several variables
|
|
|
70 |
in the load environment, which might all be named differently from the
|
| 50007 |
ripley |
71 |
data set. The third and fourth types will always result in the
|
|
|
72 |
creation of a single variable with the same name (without extension)
|
|
|
73 |
as the data set.
|
| 26383 |
hornik |
74 |
|
| 14590 |
hornik |
75 |
If no data sets are specified, \code{data} lists the available data
|
| 81303 |
smeyer |
76 |
sets. For each package,
|
|
|
77 |
it looks for a data index in the \file{Meta} subdirectory or, if
|
|
|
78 |
this is not found, scans the \file{data} subdirectory for data files
|
|
|
79 |
using \code{\link{list_files_with_type}}.
|
|
|
80 |
The information about
|
| 25080 |
hornik |
81 |
available data sets is returned in an object of class
|
| 30432 |
ripley |
82 |
\code{"packageIQR"}. The structure of this class is experimental.
|
| 30477 |
ripley |
83 |
Where the datasets have a different name from the argument that should
|
|
|
84 |
be used to retrieve them the index will have an entry like
|
|
|
85 |
\code{beaver1 (beavers)} which tells us that dataset \code{beaver1}
|
| 82811 |
smeyer |
86 |
can be retrieved by the call \code{data(beavers)}.
|
| 14590 |
hornik |
87 |
|
| 30432 |
ripley |
88 |
If \code{lib.loc} and \code{package} are both \code{NULL} (the
|
| 30446 |
ripley |
89 |
default), the data sets are searched for in all the currently loaded
|
|
|
90 |
packages then in the \file{data} directory (if any) of the current
|
|
|
91 |
working directory.
|
| 30432 |
ripley |
92 |
|
|
|
93 |
If \code{lib.loc = NULL} but \code{package} is specified as a
|
|
|
94 |
character vector, the specified package(s) are searched for first
|
| 85927 |
hornik |
95 |
amongst loaded packages and then in the default libraries
|
| 30432 |
ripley |
96 |
(see \code{\link{.libPaths}}).
|
|
|
97 |
|
|
|
98 |
If \code{lib.loc} \emph{is} specified (and not \code{NULL}), packages
|
| 85927 |
hornik |
99 |
are searched for in the specified libraries, even if they are
|
| 14590 |
hornik |
100 |
already loaded from another library.
|
| 8099 |
ripley |
101 |
|
| 9439 |
hornik |
102 |
To just look in the \file{data} directory of the current working
|
| 74831 |
hornik |
103 |
directory, set \code{package = character(0)}
|
|
|
104 |
(and \code{lib.loc = NULL}, the default).
|
| 3998 |
ripley |
105 |
}
|
| 50070 |
ripley |
106 |
\value{
|
| 76125 |
ripley |
107 |
A character vector of all data sets specified (whether found or not),
|
|
|
108 |
or information about all available data sets in an object of class
|
|
|
109 |
\code{"packageIQR"} if none were specified.
|
| 50070 |
ripley |
110 |
}
|
| 61853 |
ripley |
111 |
\section{Good practice}{
|
| 76126 |
ripley |
112 |
There is no requirement for \code{data(\var{foo})} to create an object
|
|
|
113 |
named \code{\var{foo}} (nor to create one object), although it much
|
|
|
114 |
reduces confusion if this convention is followed (and it is enforced
|
|
|
115 |
if datasets are lazy-loaded).
|
| 78171 |
maechler |
116 |
|
| 61853 |
ripley |
117 |
\code{data()} was originally intended to allow users to load datasets
|
|
|
118 |
from packages for use in their examples, and as such it loaded the
|
|
|
119 |
datasets into the workspace \code{\link{.GlobalEnv}}. This avoided
|
| 76126 |
ripley |
120 |
having large datasets in memory when not in use: that need has been
|
| 61853 |
ripley |
121 |
almost entirely superseded by lazy-loading of datasets.
|
|
|
122 |
|
|
|
123 |
The ability to specify a dataset by name (without quotes) is a
|
|
|
124 |
convenience: in programming the datasets should be specified by
|
|
|
125 |
character strings (with quotes).
|
|
|
126 |
|
|
|
127 |
Use of \code{data} within a function without an \code{envir} argument
|
|
|
128 |
has the almost always undesirable side-effect of putting an object in
|
|
|
129 |
the user's workspace (and indeed, of replacing any object of that name
|
|
|
130 |
already there). It would almost always be better to put the object in
|
| 74831 |
hornik |
131 |
the current evaluation environment by
|
|
|
132 |
\code{data(\dots, envir = environment())}.
|
|
|
133 |
However, two alternatives are usually preferable,
|
| 88907 |
smeyer |
134 |
both described in the \manual{R-exts}{} manual.
|
| 61853 |
ripley |
135 |
\itemize{
|
|
|
136 |
\item For sets of data, set up a package to use lazy-loading of data.
|
|
|
137 |
\item For objects which are system data, for example lookup tables
|
|
|
138 |
used in calculations within the function, use a file
|
|
|
139 |
\file{R/sysdata.rda} in the package sources or create the objects by
|
|
|
140 |
\R code at package installation time.
|
|
|
141 |
}
|
| 61926 |
hornik |
142 |
A sometimes important distinction is that the second approach places
|
| 61853 |
ripley |
143 |
objects in the namespace but the first does not. So if it is important
|
|
|
144 |
that the function sees \code{mytable} as an object from the package,
|
| 66525 |
ripley |
145 |
it is system data and the second approach should be used. In the
|
|
|
146 |
unusual case that a package uses a lazy-loaded dataset as a default
|
|
|
147 |
argument to a function, that needs to be specified by \code{\link{::}},
|
|
|
148 |
e.g., \code{survival::survexp.us}.
|
| 61853 |
ripley |
149 |
}
|
| 8099 |
ripley |
150 |
\note{
|
| 9472 |
pd |
151 |
One can take advantage of the search order and the fact that a
|
| 9687 |
pd |
152 |
\file{.R} file will change directory. If raw data are stored in
|
| 9472 |
pd |
153 |
\file{mydata.txt} then one can set up \file{mydata.R} to read
|
| 78171 |
maechler |
154 |
\file{mydata.txt} and pre-process it, e.g., using \code{\link{transform}()}.
|
| 14590 |
hornik |
155 |
For instance one can convert numeric vectors to factors with the
|
|
|
156 |
appropriate labels. Thus, the \file{.R} file can effectively contain
|
|
|
157 |
a metadata specification for the plaintext formats.
|
| 78171 |
maechler |
158 |
|
| 81513 |
ripley |
159 |
%% In older versions of \R, up to 3.6.x, both \code{package = "base"} and
|
|
|
160 |
%% \code{package = "stats"} were using \code{package = "datasets"}, (with a
|
|
|
161 |
%% warning), as before 2004, (most of) the datasets in \pkg{datasets} were
|
|
|
162 |
%% either in \pkg{base} or \pkg{stats}. For these packages, the result
|
|
|
163 |
%% is now empty as they contain no data sets.
|
| 8099 |
ripley |
164 |
}
|
| 76108 |
ripley |
165 |
\section{Warning}{
|
|
|
166 |
This function creates objects in the \code{envir} environment (by
|
|
|
167 |
default the user's workspace) replacing any which already
|
|
|
168 |
existed. \code{data("foo")} can silently create objects other than
|
|
|
169 |
\code{foo}: there have been instances in published packages where it
|
|
|
170 |
created/replaced \code{\link{.Random.seed}} and hence change the seed
|
|
|
171 |
for the session.
|
|
|
172 |
}
|
| 2 |
r |
173 |
\seealso{
|
| 30461 |
ripley |
174 |
\code{\link{help}} for obtaining documentation on data sets,
|
| 25080 |
hornik |
175 |
\code{\link{save}} for \emph{creating} the second (\file{.rda}) kind
|
|
|
176 |
of data, typically the most efficient one.
|
| 61433 |
ripley |
177 |
|
| 88907 |
smeyer |
178 |
Section \manual{R-exts}{Data in packages} for considerations in preparing the
|
| 50070 |
ripley |
179 |
\file{data} directory of a package.
|
| 2 |
r |
180 |
}
|
|
|
181 |
\examples{
|
| 27541 |
ripley |
182 |
require(utils)
|
| 61168 |
ripley |
183 |
data() # list all available data sets
|
| 80290 |
ripley |
184 |
try(data(package = "rpart"), silent = TRUE) # list the data sets in the rpart package
|
| 61168 |
ripley |
185 |
data(USArrests, "VADeaths") # load the data sets 'USArrests' and 'VADeaths'
|
| 61596 |
ripley |
186 |
\dontrun{## Alternatively
|
|
|
187 |
ds <- c("USArrests", "VADeaths"); data(list = ds)}
|
| 61168 |
ripley |
188 |
help(USArrests) # give information on data set 'USArrests'
|
| 2 |
r |
189 |
}
|
| 975 |
maechler |
190 |
\keyword{documentation}
|
| 286 |
maechler |
191 |
\keyword{datasets}
|