/
index.html
256 lines (238 loc) · 18.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Read Rectangular Text Data • readr</title>
<!-- jquery --><script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha384-nrOSfDHtoPMzJHjVTdCopGqIqeYETSXhZDFyniQ8ZHcVy08QesyHcnOUpMpqnmWq" crossorigin="anonymous"></script><!-- Bootstrap --><link href="tidyverse.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script><!-- Font Awesome icons --><link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous">
<!-- tidyverse --><link href="pkgdown.css" rel="stylesheet">
<script src="jquery.sticky-kit.min.js"></script><script src="pkgdown.js"></script><!-- mathjax --><script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container template-vignette">
<header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="navbar-brand-container">
<a class="navbar-brand" href="index.html">readr</a>
<small class="tidyverse">part of the <a href="http://tidyverse.org">tidyverse</a></small>
</div>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="articles/readr.html">Overview</a>
</li>
<li>
<a href="articles/locales.html">Locales</a>
</li>
<li>
<a href="reference/index.html">Reference</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
News
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li class="dropdown-header">Release notes</li>
<li>
<a href="articles/releases/readr-1.0.0.html">Version 1.0.0</a>
</li>
<li>
<a href="articles/releases/readr-0.2.0.html">Version 0.2.0</a>
</li>
<li>
<a href="articles/releases/readr-0.1.0.html">Version 0.1.0</a>
</li>
<li class="divider">
</li>
<li>
<a href="news/index.html">Change log</a>
</li>
</ul>
</li>
<li>
<a href="https://github.com/tidyverse/readr">
<span class="fa fa-github fa-lg"></span>
</a>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container -->
</div>
<!--/.navbar -->
</header><div class="row">
<div class="col-md-9">
<div class="contents">
<!-- README.md is generated from README.Rmd. Please edit that file -->
<section id="readr" class="level1">
<section id="overview" class="level2"><h2>Overview</h2>
<p>The goal of readr is to provide a fast and friendly way to read rectangular data (like csv, tsv, and fwf). It is designed to flexibly parse many types of data found in the wild, while still cleanly failing when data unexpectedly changes. If you are new to readr, the best place to start is the <a href="http://r4ds.had.co.nz/data-import.html">data import chapter</a> in R for data science.</p>
</section><section id="installation" class="level2"><h2>Installation</h2>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="co"># The easiest way to get readr is to install the whole tidyverse:</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="kw">install.packages</span>(<span class="st">"tidyverse"</span>)</a>
<a class="sourceLine" id="cb1-3" data-line-number="3"></a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="co"># Alternatively, install just readr:</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5"><span class="kw">install.packages</span>(<span class="st">"readr"</span>)</a>
<a class="sourceLine" id="cb1-6" data-line-number="6"></a>
<a class="sourceLine" id="cb1-7" data-line-number="7"><span class="co"># Or the the development version from GitHub:</span></a>
<a class="sourceLine" id="cb1-8" data-line-number="8"><span class="co"># install.packages("devtools")</span></a>
<a class="sourceLine" id="cb1-9" data-line-number="9">devtools<span class="op">::</span><span class="kw"><a href="http://www.rdocumentation.org/packages/devtools/topics/install_github">install_github</a></span>(<span class="st">"tidyverse/readr"</span>)</a></code></pre></div>
</section><section id="usage" class="level2"><h2>Usage</h2>
<p>readr is part of the core tidyverse, so load it with:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="kw">library</span>(tidyverse)</a>
<a class="sourceLine" id="cb2-2" data-line-number="2"><span class="co">#> ── Attaching packages ────────────────────────────────── tidyverse 1.2.1 ──</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3"><span class="co">#> ✔ ggplot2 2.2.1 ✔ purrr 0.2.4 </span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4"><span class="co">#> ✔ tibble 1.4.1 ✔ dplyr 0.7.4 </span></a>
<a class="sourceLine" id="cb2-5" data-line-number="5"><span class="co">#> ✔ tidyr 0.7.2 ✔ stringr 1.2.0 </span></a>
<a class="sourceLine" id="cb2-6" data-line-number="6"><span class="co">#> ✔ readr 1.1.1.9000 ✔ forcats 0.2.0</span></a>
<a class="sourceLine" id="cb2-7" data-line-number="7"><span class="co">#> ── Conflicts ───────────────────────────────────── tidyverse_conflicts() ──</span></a>
<a class="sourceLine" id="cb2-8" data-line-number="8"><span class="co">#> ✖ dplyr::filter() masks stats::filter()</span></a>
<a class="sourceLine" id="cb2-9" data-line-number="9"><span class="co">#> ✖ dplyr::lag() masks stats::lag()</span></a></code></pre></div>
<p>To accurately read a rectangular dataset with readr you combine two pieces: a function that parses the overall file, and a column specification. The column specification describes how each column should be converted from a character vector to the most appropriate data type, and in most cases it’s not necessary because readr will guess it for you automatically.</p>
<p>readr supports seven file formats with seven <code>read_</code> functions:</p>
<ul>
<li>
<code><a href="reference/read_delim.html">read_csv()</a></code>: comma separated (CSV) files</li>
<li>
<code><a href="reference/read_delim.html">read_tsv()</a></code>: tab separated files</li>
<li>
<code><a href="reference/read_delim.html">read_delim()</a></code>: general delimited files</li>
<li>
<code><a href="reference/read_fwf.html">read_fwf()</a></code>: fixed width files</li>
<li>
<code><a href="reference/read_table.html">read_table()</a></code>: tabular files where colums are separated by white-space.</li>
<li>
<code><a href="reference/read_log.html">read_log()</a></code>: web log files</li>
</ul>
<p>In many cases, these functions will just work: you supply the path to a file and you get a tibble back. The following example loads a sample file bundled with readr:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb3-1" data-line-number="1">mtcars <-<span class="st"> </span><span class="kw"><a href="reference/read_delim.html">read_csv</a></span>(<span class="kw"><a href="reference/readr_example.html">readr_example</a></span>(<span class="st">"mtcars.csv"</span>))</a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="co">#> Parsed with column specification:</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3"><span class="co">#> cols(</span></a>
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="co">#> mpg = col_double(),</span></a>
<a class="sourceLine" id="cb3-5" data-line-number="5"><span class="co">#> cyl = col_double(),</span></a>
<a class="sourceLine" id="cb3-6" data-line-number="6"><span class="co">#> disp = col_double(),</span></a>
<a class="sourceLine" id="cb3-7" data-line-number="7"><span class="co">#> hp = col_double(),</span></a>
<a class="sourceLine" id="cb3-8" data-line-number="8"><span class="co">#> drat = col_double(),</span></a>
<a class="sourceLine" id="cb3-9" data-line-number="9"><span class="co">#> wt = col_double(),</span></a>
<a class="sourceLine" id="cb3-10" data-line-number="10"><span class="co">#> qsec = col_double(),</span></a>
<a class="sourceLine" id="cb3-11" data-line-number="11"><span class="co">#> vs = col_double(),</span></a>
<a class="sourceLine" id="cb3-12" data-line-number="12"><span class="co">#> am = col_double(),</span></a>
<a class="sourceLine" id="cb3-13" data-line-number="13"><span class="co">#> gear = col_double(),</span></a>
<a class="sourceLine" id="cb3-14" data-line-number="14"><span class="co">#> carb = col_double()</span></a>
<a class="sourceLine" id="cb3-15" data-line-number="15"><span class="co">#> )</span></a></code></pre></div>
<p>Note that readr prints the column specification. This is useful because it allows you to check that the columns have been read in as you expect, and if they haven’t, you can easily copy and paste into a new call:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb4-1" data-line-number="1">mtcars <-<span class="st"> </span><span class="kw"><a href="reference/read_delim.html">read_csv</a></span>(<span class="kw"><a href="reference/readr_example.html">readr_example</a></span>(<span class="st">"mtcars.csv"</span>), <span class="dt">col_types =</span> </a>
<a class="sourceLine" id="cb4-2" data-line-number="2"> <span class="kw"><a href="reference/cols.html">cols</a></span>(</a>
<a class="sourceLine" id="cb4-3" data-line-number="3"> <span class="dt">mpg =</span> <span class="kw"><a href="reference/parse_atomic.html">col_double</a></span>(),</a>
<a class="sourceLine" id="cb4-4" data-line-number="4"> <span class="dt">cyl =</span> <span class="kw"><a href="reference/parse_atomic.html">col_integer</a></span>(),</a>
<a class="sourceLine" id="cb4-5" data-line-number="5"> <span class="dt">disp =</span> <span class="kw"><a href="reference/parse_atomic.html">col_double</a></span>(),</a>
<a class="sourceLine" id="cb4-6" data-line-number="6"> <span class="dt">hp =</span> <span class="kw"><a href="reference/parse_atomic.html">col_integer</a></span>(),</a>
<a class="sourceLine" id="cb4-7" data-line-number="7"> <span class="dt">drat =</span> <span class="kw"><a href="reference/parse_atomic.html">col_double</a></span>(),</a>
<a class="sourceLine" id="cb4-8" data-line-number="8"> <span class="dt">vs =</span> <span class="kw"><a href="reference/parse_atomic.html">col_integer</a></span>(),</a>
<a class="sourceLine" id="cb4-9" data-line-number="9"> <span class="dt">wt =</span> <span class="kw"><a href="reference/parse_atomic.html">col_double</a></span>(),</a>
<a class="sourceLine" id="cb4-10" data-line-number="10"> <span class="dt">qsec =</span> <span class="kw"><a href="reference/parse_atomic.html">col_double</a></span>(),</a>
<a class="sourceLine" id="cb4-11" data-line-number="11"> <span class="dt">am =</span> <span class="kw"><a href="reference/parse_atomic.html">col_integer</a></span>(),</a>
<a class="sourceLine" id="cb4-12" data-line-number="12"> <span class="dt">gear =</span> <span class="kw"><a href="reference/parse_atomic.html">col_integer</a></span>(),</a>
<a class="sourceLine" id="cb4-13" data-line-number="13"> <span class="dt">carb =</span> <span class="kw"><a href="reference/parse_atomic.html">col_integer</a></span>()</a>
<a class="sourceLine" id="cb4-14" data-line-number="14"> )</a>
<a class="sourceLine" id="cb4-15" data-line-number="15">)</a></code></pre></div>
<p><code><a href="articles/readr.html">vignette("readr")</a></code> gives more detail on how readr guess the column types, how you can override the defaults, and provides some useful tools for debugging parsing problems.</p>
</section><section id="alternatives" class="level2"><h2>Alternatives</h2>
<p>There are two main alternatives to readr: base R and data.table’s <code>fread()</code>. The most important differences are discussed below.</p>
<section id="base-r" class="level3"><h3>Base R</h3>
<p>Compared to the corresponding base functions, readr functions:</p>
<ul>
<li><p>Use a consistent naming scheme for the parameters (e.g. <code>col_names</code> and <code>col_types</code> not <code>header</code> and <code>colClasses</code>).</p></li>
<li><p>Are much faster (up to 10x).</p></li>
<li><p>Leave strings as is by default, and automatically parse common date/time formats.</p></li>
<li><p>Have a helpful progress bar if loading is going to take a while.</p></li>
<li><p>All functions work exactly the same way regardless of the current locale. To override the US-centric defaults, use <code><a href="reference/locale.html">locale()</a></code>.</p></li>
</ul></section><section id="data.table-and-fread" class="level3"><h3>data.table and <code>fread()</code>
</h3>
<p><a href="https://github.com/Rdatatable/data.table">data.table</a> has a function similar to <code><a href="reference/read_delim.html">read_csv()</a></code> called fread. Compared to fread, readr functions:</p>
<ul>
<li><p>Are slower (currently ~1.2-2x slower. If you want absolutely the best performance, use <code><a href="http://www.rdocumentation.org/packages/data.table/topics/fread">data.table::fread()</a></code>.</p></li>
<li><p>Use a slightly more sophisticated parser, recognising both doubled (<code>""""</code>) and backslash escapes (<code>"\""</code>), and can produce factors and date/times directly.</p></li>
<li><p>Forces you to supply all parameters, where <code>fread()</code> saves you work by automatically guessing the delimiter, whether or not the file has a header, and how many lines to skip.</p></li>
<li><p>Are built on a different underlying infrastructure. Readr functions are designed to be quite general, which makes it easier to add support for new rectangular data formats. <code>fread()</code> is designed to be as fast as possible.</p></li>
</ul></section></section><section id="acknowledgements" class="level2"><h2>Acknowledgements</h2>
<p>Thanks to:</p>
<ul>
<li><p><a href="https://github.com/jcheng5">Joe Cheng</a> for showing me the beauty of deterministic finite automata for parsing, and for teaching me why I should write a tokenizer.</p></li>
<li><p><a href="https://github.com/jjallaire">JJ Allaire</a> for helping me come up with a design that makes very few copies, and is easy to extend.</p></li>
<li><p><a href="http://dirk.eddelbuettel.com">Dirk Eddelbuettel</a> for coming up with the name!</p></li>
</ul>
<p>Please note that this project is released with a <a href="CONDUCT.md">Contributor Code of Conduct</a>. By participating in this project you agree to abide by its terms.</p>
</section></section>
</div>
</div>
<div class="col-md-3 hidden-xs hidden-sm" id="sidebar">
<h2 class="hasAnchor">
<a href="#sidebar" class="anchor"></a>Links</h2>
<ul class="list-unstyled">
<li>Download from CRAN at <br><a href="https://cran.r-project.org/package=readr">https://cran.r-project.org/package=readr</a>
</li>
<li>Browse source code at <br><a href="https://github.com/tidyverse/readr">https://github.com/tidyverse/readr</a>
</li>
<li>Report a bug at <br><a href="https://github.com/tidyverse/readr/issues">https://github.com/tidyverse/readr/issues</a>
</li>
<li>Learn more at <br><a href="http://r4ds.had.co.nz/data-import.html">http://r4ds.had.co.nz/data-import.html</a>
</li>
</ul>
<h2>License</h2>
<p>GPL (>= 2) | file <a href="LICENSE.html">LICENSE</a></p>
<h2>Developers</h2>
<ul class="list-unstyled">
<li>
<a href="http://hadley.nz">Hadley Wickham</a> <br><small class="roles"> Author </small> </li>
<li>Jim Hester <br><small class="roles"> Author, maintainer </small> </li>
<li>Romain Francois <br><small class="roles"> Author </small> </li>
<li>
<a href="https://www.rstudio.com"><img src="http://tidyverse.org/rstudio-logo.svg" height="24"></a> <br><small class="roles"> Copyright holder, funder </small> </li>
<li><a href="authors.html">All authors...</a></li>
</ul>
<h2>Dev status</h2>
<ul class="list-unstyled">
<li><a href="http://cran.r-project.org/package=readr"><img src="http://www.r-pkg.org/badges/version/readr" alt="CRAN_Status_Badge"></a></li>
<li><a href="https://travis-ci.org/tidyverse/readr"><img src="https://travis-ci.org/tidyverse/readr.svg?branch=master" alt="Build Status"></a></li>
<li><a href="https://ci.appveyor.com/project/tidyverse/readr"><img src="https://ci.appveyor.com/api/projects/status/github/tidyverse/readr?branch=master&svg=true" alt="AppVeyor Build Status"></a></li>
<li><a href="http://codecov.io/tidyverse/readr?branch=master"><img src="http://codecov.io/github/tidyverse/readr/coverage.svg?branch=master" alt="Coverage Status"></a></li>
</ul>
</div>
</div>
<footer><div class="tidyverse">
<p>readr is a part of the <strong>tidyverse</strong>, an ecosystem of packages designed with common APIs and a shared philosophy. Learn more at <a href="http://tidyverse.org">tidyverse.org</a>.</p>
</div>
<div class="author">
<p>Developed by <a href="http://hadley.nz">Hadley Wickham</a>, Jim Hester, Romain Francois, <a href="https://www.rstudio.com"><img src="http://tidyverse.org/rstudio-logo.svg" height="24"></a>.</p>
<p>Site built by <a href="http://hadley.github.io/pkgdown/">pkgdown</a>.</p>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-67989-18', 'auto');
ga('send', 'pageview');
</script></footer>
</div>
</body>
</html>