| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
|
| 5 |
<head> |
| 6 |
|
| 7 |
<meta charset="utf-8" /> |
| 8 |
<meta name="generator" content="pandoc" /> |
| 9 |
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" /> |
| 10 |
|
| 11 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
<title>Comparison of fs functions, base R, and shell commands</title> |
| 16 |
|
| 17 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 18 |
// be compatible with the behavior of Pandoc < 2.8). |
| 19 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 20 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 21 |
var i, h, a; |
| 22 |
for (i = 0; i < hs.length; i++) { |
| 23 |
h = hs[i]; |
| 24 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 25 |
a = h.attributes; |
| 26 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 27 |
} |
| 28 |
}); |
| 29 |
</script> |
| 30 |
|
| 31 |
<style type="text/css"> |
| 32 |
code{white-space: pre-wrap;} |
| 33 |
span.smallcaps{font-variant: small-caps;} |
| 34 |
span.underline{text-decoration: underline;} |
| 35 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 36 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 37 |
ul.task-list{list-style: none;} |
| 38 |
</style> |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
<style type="text/css">body { |
| 47 |
background-color: #fff; |
| 48 |
margin: 1em auto; |
| 49 |
max-width: 700px; |
| 50 |
overflow: visible; |
| 51 |
padding-left: 2em; |
| 52 |
padding-right: 2em; |
| 53 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 54 |
font-size: 14px; |
| 55 |
line-height: 1.35; |
| 56 |
} |
| 57 |
#TOC { |
| 58 |
clear: both; |
| 59 |
margin: 0 0 10px 10px; |
| 60 |
padding: 4px; |
| 61 |
width: 400px; |
| 62 |
border: 1px solid #CCCCCC; |
| 63 |
border-radius: 5px; |
| 64 |
background-color: #f6f6f6; |
| 65 |
font-size: 13px; |
| 66 |
line-height: 1.3; |
| 67 |
} |
| 68 |
#TOC .toctitle { |
| 69 |
font-weight: bold; |
| 70 |
font-size: 15px; |
| 71 |
margin-left: 5px; |
| 72 |
} |
| 73 |
#TOC ul { |
| 74 |
padding-left: 40px; |
| 75 |
margin-left: -1.5em; |
| 76 |
margin-top: 5px; |
| 77 |
margin-bottom: 5px; |
| 78 |
} |
| 79 |
#TOC ul ul { |
| 80 |
margin-left: -2em; |
| 81 |
} |
| 82 |
#TOC li { |
| 83 |
line-height: 16px; |
| 84 |
} |
| 85 |
table { |
| 86 |
margin: 1em auto; |
| 87 |
border-width: 1px; |
| 88 |
border-color: #DDDDDD; |
| 89 |
border-style: outset; |
| 90 |
border-collapse: collapse; |
| 91 |
} |
| 92 |
table th { |
| 93 |
border-width: 2px; |
| 94 |
padding: 5px; |
| 95 |
border-style: inset; |
| 96 |
} |
| 97 |
table td { |
| 98 |
border-width: 1px; |
| 99 |
border-style: inset; |
| 100 |
line-height: 18px; |
| 101 |
padding: 5px 5px; |
| 102 |
} |
| 103 |
table, table th, table td { |
| 104 |
border-left-style: none; |
| 105 |
border-right-style: none; |
| 106 |
} |
| 107 |
table thead, table tr.even { |
| 108 |
background-color: #f7f7f7; |
| 109 |
} |
| 110 |
p { |
| 111 |
margin: 0.5em 0; |
| 112 |
} |
| 113 |
blockquote { |
| 114 |
background-color: #f6f6f6; |
| 115 |
padding: 0.25em 0.75em; |
| 116 |
} |
| 117 |
hr { |
| 118 |
border-style: solid; |
| 119 |
border: none; |
| 120 |
border-top: 1px solid #777; |
| 121 |
margin: 28px 0; |
| 122 |
} |
| 123 |
dl { |
| 124 |
margin-left: 0; |
| 125 |
} |
| 126 |
dl dd { |
| 127 |
margin-bottom: 13px; |
| 128 |
margin-left: 13px; |
| 129 |
} |
| 130 |
dl dt { |
| 131 |
font-weight: bold; |
| 132 |
} |
| 133 |
ul { |
| 134 |
margin-top: 0; |
| 135 |
} |
| 136 |
ul li { |
| 137 |
list-style: circle outside; |
| 138 |
} |
| 139 |
ul ul { |
| 140 |
margin-bottom: 0; |
| 141 |
} |
| 142 |
pre, code { |
| 143 |
background-color: #f7f7f7; |
| 144 |
border-radius: 3px; |
| 145 |
color: #333; |
| 146 |
white-space: pre-wrap; |
| 147 |
} |
| 148 |
pre { |
| 149 |
border-radius: 3px; |
| 150 |
margin: 5px 0px 10px 0px; |
| 151 |
padding: 10px; |
| 152 |
} |
| 153 |
pre:not([class]) { |
| 154 |
background-color: #f7f7f7; |
| 155 |
} |
| 156 |
code { |
| 157 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 158 |
font-size: 85%; |
| 159 |
} |
| 160 |
p > code, li > code { |
| 161 |
padding: 2px 0px; |
| 162 |
} |
| 163 |
div.figure { |
| 164 |
text-align: center; |
| 165 |
} |
| 166 |
img { |
| 167 |
background-color: #FFFFFF; |
| 168 |
padding: 2px; |
| 169 |
border: 1px solid #DDDDDD; |
| 170 |
border-radius: 3px; |
| 171 |
border: 1px solid #CCCCCC; |
| 172 |
margin: 0 5px; |
| 173 |
} |
| 174 |
h1 { |
| 175 |
margin-top: 0; |
| 176 |
font-size: 35px; |
| 177 |
line-height: 40px; |
| 178 |
} |
| 179 |
h2 { |
| 180 |
border-bottom: 4px solid #f7f7f7; |
| 181 |
padding-top: 10px; |
| 182 |
padding-bottom: 2px; |
| 183 |
font-size: 145%; |
| 184 |
} |
| 185 |
h3 { |
| 186 |
border-bottom: 2px solid #f7f7f7; |
| 187 |
padding-top: 10px; |
| 188 |
font-size: 120%; |
| 189 |
} |
| 190 |
h4 { |
| 191 |
border-bottom: 1px solid #f7f7f7; |
| 192 |
margin-left: 8px; |
| 193 |
font-size: 105%; |
| 194 |
} |
| 195 |
h5, h6 { |
| 196 |
border-bottom: 1px solid #ccc; |
| 197 |
font-size: 105%; |
| 198 |
} |
| 199 |
a { |
| 200 |
color: #0033dd; |
| 201 |
text-decoration: none; |
| 202 |
} |
| 203 |
a:hover { |
| 204 |
color: #6666ff; } |
| 205 |
a:visited { |
| 206 |
color: #800080; } |
| 207 |
a:visited:hover { |
| 208 |
color: #BB00BB; } |
| 209 |
a[href^="http:"] { |
| 210 |
text-decoration: underline; } |
| 211 |
a[href^="https:"] { |
| 212 |
text-decoration: underline; } |
| 213 |
|
| 214 |
code > span.kw { color: #555; font-weight: bold; } |
| 215 |
code > span.dt { color: #902000; } |
| 216 |
code > span.dv { color: #40a070; } |
| 217 |
code > span.bn { color: #d14; } |
| 218 |
code > span.fl { color: #d14; } |
| 219 |
code > span.ch { color: #d14; } |
| 220 |
code > span.st { color: #d14; } |
| 221 |
code > span.co { color: #888888; font-style: italic; } |
| 222 |
code > span.ot { color: #007020; } |
| 223 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 224 |
code > span.fu { color: #900; font-weight: bold; } |
| 225 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 226 |
</style> |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
</head> |
| 232 |
|
| 233 |
<body> |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
<h1 class="title toc-ignore">Comparison of fs functions, base R, and |
| 239 |
shell commands</h1> |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
<p>Most of the functionality is <strong>fs</strong> can be approximated |
| 244 |
with functions in <strong>base R</strong> or in a command line shell. |
| 245 |
The table at the end of this vignette can be used as a translation aid |
| 246 |
between these three methods.</p> |
| 247 |
<p><strong>fs</strong> functions smooth over some of the idiosyncrasies |
| 248 |
of file handling with base R functions:</p> |
| 249 |
<ul> |
| 250 |
<li><p>Vectorization. All <strong>fs</strong> functions are vectorized, |
| 251 |
accepting multiple paths as input. Base functions are inconsistently |
| 252 |
vectorized.</p></li> |
| 253 |
<li><p>Predictable return values that always convey a path. All |
| 254 |
<strong>fs</strong> functions return a character vector of paths, a |
| 255 |
named integer or a logical vector, where the names give the paths. Base |
| 256 |
return values are more varied: they are often logical or contain error |
| 257 |
codes which require downstream processing.</p></li> |
| 258 |
<li><p>Explicit failure. If <strong>fs</strong> operations fail, they |
| 259 |
throw an error. Base functions tend to generate a warning and a system |
| 260 |
dependent error code. This makes it easy to miss a failure.</p></li> |
| 261 |
<li><p>UTF-8 all the things. <strong>fs</strong> functions always |
| 262 |
convert input paths to UTF-8 and return results as UTF-8. This gives you |
| 263 |
path encoding consistency across OSes. Base functions rely on the native |
| 264 |
system encoding.</p></li> |
| 265 |
<li><p>Naming convention. <strong>fs</strong> functions use a consistent |
| 266 |
naming convention. Because base R’s functions were gradually added over |
| 267 |
time there are a number of different conventions used |
| 268 |
(e.g. <code>path.expand()</code> vs <code>normalizePath()</code>; |
| 269 |
<code>Sys.chmod()</code> vs <code>file.access()</code>).</p></li> |
| 270 |
</ul> |
| 271 |
<div id="directory-functions" class="section level2"> |
| 272 |
<h2>Directory functions</h2> |
| 273 |
<table> |
| 274 |
<colgroup> |
| 275 |
<col width="33%" /> |
| 276 |
<col width="33%" /> |
| 277 |
<col width="33%" /> |
| 278 |
</colgroup> |
| 279 |
<thead> |
| 280 |
<tr class="header"> |
| 281 |
<th>fs</th> |
| 282 |
<th>base</th> |
| 283 |
<th>shell</th> |
| 284 |
</tr> |
| 285 |
</thead> |
| 286 |
<tbody> |
| 287 |
<tr class="odd"> |
| 288 |
<td><code>dir_ls("path")</code></td> |
| 289 |
<td><code>list.files("path")</code></td> |
| 290 |
<td><code>ls path</code></td> |
| 291 |
</tr> |
| 292 |
<tr class="even"> |
| 293 |
<td><code>dir_info("path")</code></td> |
| 294 |
<td><code>file.info(list.files("path", full.names = TRUE))</code></td> |
| 295 |
<td><code>ls -al path</code></td> |
| 296 |
</tr> |
| 297 |
<tr class="odd"> |
| 298 |
<td><code>dir_copy("path", "new-path")</code></td> |
| 299 |
<td><code>dir.create("new-path"); file.copy("path", "new-path", recursive=TRUE)</code></td> |
| 300 |
<td><code>cp path new-path</code></td> |
| 301 |
</tr> |
| 302 |
<tr class="even"> |
| 303 |
<td><code>dir_create("path")</code></td> |
| 304 |
<td><code>dir.create("path")</code></td> |
| 305 |
<td><code>mkdir path</code></td> |
| 306 |
</tr> |
| 307 |
<tr class="odd"> |
| 308 |
<td><code>dir_delete("path")</code></td> |
| 309 |
<td><code>unlink("path", recursive = TRUE)</code></td> |
| 310 |
<td><code>rm -rf path</code></td> |
| 311 |
</tr> |
| 312 |
<tr class="even"> |
| 313 |
<td><code>dir_exists("path")</code></td> |
| 314 |
<td><code>dir.exists("path")</code></td> |
| 315 |
<td><code>if [ -d "path" ]; then ... ; fi</code></td> |
| 316 |
</tr> |
| 317 |
<tr class="odd"> |
| 318 |
<td><del><code>dir_move()</code></del> (see <code>file_move</code>)</td> |
| 319 |
<td><code>file.rename("path", "new-path")</code></td> |
| 320 |
<td><code>mv path new-path</code></td> |
| 321 |
</tr> |
| 322 |
<tr class="even"> |
| 323 |
<td><code>dir_map("path", fun)</code></td> |
| 324 |
<td><em>No direct equivalent</em></td> |
| 325 |
<td><code>for file in $(ls path); do ...; done</code></td> |
| 326 |
</tr> |
| 327 |
<tr class="odd"> |
| 328 |
<td><code>dir_tree("path")</code></td> |
| 329 |
<td><em>No direct equivalent</em></td> |
| 330 |
<td><code>tree path</code></td> |
| 331 |
</tr> |
| 332 |
</tbody> |
| 333 |
</table> |
| 334 |
</div> |
| 335 |
<div id="file-functions" class="section level2"> |
| 336 |
<h2>File functions</h2> |
| 337 |
<table> |
| 338 |
<colgroup> |
| 339 |
<col width="33%" /> |
| 340 |
<col width="33%" /> |
| 341 |
<col width="33%" /> |
| 342 |
</colgroup> |
| 343 |
<thead> |
| 344 |
<tr class="header"> |
| 345 |
<th>fs</th> |
| 346 |
<th>base</th> |
| 347 |
<th>shell</th> |
| 348 |
</tr> |
| 349 |
</thead> |
| 350 |
<tbody> |
| 351 |
<tr class="odd"> |
| 352 |
<td><code>file_chmod("path", "mode")</code></td> |
| 353 |
<td><code>Sys.chmod("path", "mode")</code></td> |
| 354 |
<td><code>chmod mode path</code></td> |
| 355 |
</tr> |
| 356 |
<tr class="even"> |
| 357 |
<td><code>file_chown("path", "user_id", "group_id")</code></td> |
| 358 |
<td><em>No direct equivalent</em></td> |
| 359 |
<td><code>chown options path</code></td> |
| 360 |
</tr> |
| 361 |
<tr class="odd"> |
| 362 |
<td><code>file_copy("path", "new-path")</code></td> |
| 363 |
<td><code>file.copy("path", "new-path")</code></td> |
| 364 |
<td><code>cp path new-path</code></td> |
| 365 |
</tr> |
| 366 |
<tr class="even"> |
| 367 |
<td><code>file_create("new-path")</code></td> |
| 368 |
<td><code>file.create("new-path")</code></td> |
| 369 |
<td><code>touch new-path</code></td> |
| 370 |
</tr> |
| 371 |
<tr class="odd"> |
| 372 |
<td><code>file_delete("path")</code></td> |
| 373 |
<td><code>unlink("path")</code></td> |
| 374 |
<td><code>rm path</code></td> |
| 375 |
</tr> |
| 376 |
<tr class="even"> |
| 377 |
<td><code>file_exists("path")</code></td> |
| 378 |
<td><code>file.exists("path")</code></td> |
| 379 |
<td><code>if [ -f "path" ]; then ... ; fi</code></td> |
| 380 |
</tr> |
| 381 |
<tr class="odd"> |
| 382 |
<td><code>file_info("path")</code></td> |
| 383 |
<td><code>file.info("path")</code></td> |
| 384 |
<td><code>ls -al path</code></td> |
| 385 |
</tr> |
| 386 |
<tr class="even"> |
| 387 |
<td><code>file_move("path", "new-path")</code></td> |
| 388 |
<td><code>file.rename("path", "new-path")</code></td> |
| 389 |
<td><code>mv path new-path</code></td> |
| 390 |
</tr> |
| 391 |
<tr class="odd"> |
| 392 |
<td><code>file_show("path")</code></td> |
| 393 |
<td><code>browseURL("path")</code></td> |
| 394 |
<td><code>open path</code></td> |
| 395 |
</tr> |
| 396 |
<tr class="even"> |
| 397 |
<td><code>file_touch()</code></td> |
| 398 |
<td><em>No direct equivalent</em></td> |
| 399 |
<td><code>touch path</code></td> |
| 400 |
</tr> |
| 401 |
<tr class="odd"> |
| 402 |
<td><code>file_temp()</code></td> |
| 403 |
<td><code>tempfile()</code></td> |
| 404 |
<td><code>mktemp</code></td> |
| 405 |
</tr> |
| 406 |
<tr class="even"> |
| 407 |
<td><em>No direct equivalent</em></td> |
| 408 |
<td><code>file_test()</code></td> |
| 409 |
<td><code>if [ -d "path" ]; then ...; fi</code></td> |
| 410 |
</tr> |
| 411 |
</tbody> |
| 412 |
</table> |
| 413 |
</div> |
| 414 |
<div id="path-functions" class="section level2"> |
| 415 |
<h2>Path functions</h2> |
| 416 |
<table> |
| 417 |
<colgroup> |
| 418 |
<col width="33%" /> |
| 419 |
<col width="33%" /> |
| 420 |
<col width="33%" /> |
| 421 |
</colgroup> |
| 422 |
<thead> |
| 423 |
<tr class="header"> |
| 424 |
<th>fs</th> |
| 425 |
<th>base</th> |
| 426 |
<th>shell</th> |
| 427 |
</tr> |
| 428 |
</thead> |
| 429 |
<tbody> |
| 430 |
<tr class="odd"> |
| 431 |
<td><code>path("top_dir", "nested_dir", "file", ext = "ext")</code></td> |
| 432 |
<td><code>file.path("top_dir", "nested_dir", "file.ext")</code></td> |
| 433 |
<td><code>top_dir/nested_dir/file.ext</code></td> |
| 434 |
</tr> |
| 435 |
<tr class="even"> |
| 436 |
<td><code>path_temp()</code>, <code>path_temp("path")</code></td> |
| 437 |
<td><code>tempdir()</code>, |
| 438 |
<code>file.path(tempdir(), "path")</code></td> |
| 439 |
<td><code>mktemp -d</code></td> |
| 440 |
</tr> |
| 441 |
<tr class="odd"> |
| 442 |
<td><code>path_expand("~/path")</code></td> |
| 443 |
<td><code>path.expand()</code></td> |
| 444 |
<td><code>realpath -m -s ~/path</code></td> |
| 445 |
</tr> |
| 446 |
<tr class="even"> |
| 447 |
<td><code>path_dir("path")</code></td> |
| 448 |
<td><code>dirname("path")</code></td> |
| 449 |
<td><code>dirname path</code></td> |
| 450 |
</tr> |
| 451 |
<tr class="odd"> |
| 452 |
<td><code>path_file("path")</code></td> |
| 453 |
<td><code>basename("path")</code></td> |
| 454 |
<td><code>basename path</code></td> |
| 455 |
</tr> |
| 456 |
<tr class="even"> |
| 457 |
<td><code>path_home()</code></td> |
| 458 |
<td><code>path.expand("~")</code></td> |
| 459 |
<td><code>$HOME</code></td> |
| 460 |
</tr> |
| 461 |
<tr class="odd"> |
| 462 |
<td><code>path_package("pkgname", "dir", "file")</code></td> |
| 463 |
<td><code>system.file("dir", "file", package = "pkgname")</code></td> |
| 464 |
<td><em>No direct equivalent</em></td> |
| 465 |
</tr> |
| 466 |
<tr class="even"> |
| 467 |
<td><code>path_norm("path")</code></td> |
| 468 |
<td><code>normalizePath()</code></td> |
| 469 |
<td><code>realpath</code></td> |
| 470 |
</tr> |
| 471 |
<tr class="odd"> |
| 472 |
<td><code>path_real("path")</code></td> |
| 473 |
<td><code>normalizePath(mustWork = TRUE)</code></td> |
| 474 |
<td><code>realpath</code></td> |
| 475 |
</tr> |
| 476 |
<tr class="even"> |
| 477 |
<td><code>path_rel("path/foo", "path/bar")</code></td> |
| 478 |
<td><em>No direct equivalent</em></td> |
| 479 |
<td><em>No direct equivalent</em></td> |
| 480 |
</tr> |
| 481 |
<tr class="odd"> |
| 482 |
<td><code>path_common(c("path/foo", "path/bar", "path/baz"))</code></td> |
| 483 |
<td><em>No direct equivalent</em></td> |
| 484 |
<td><em>No direct equivalent</em></td> |
| 485 |
</tr> |
| 486 |
<tr class="even"> |
| 487 |
<td><code>path_ext_remove("path")</code></td> |
| 488 |
<td><code>sub("\\.[a-zA-Z0-9]*$", "", "path")</code></td> |
| 489 |
<td><em>No direct equivalent</em></td> |
| 490 |
</tr> |
| 491 |
<tr class="odd"> |
| 492 |
<td><code>path_ext_set("path", "new_ext")</code></td> |
| 493 |
<td><code>sub("\\.[a-zA-Z0-9]*$", "new_ext", "path")</code></td> |
| 494 |
<td><em>No direct equivalent</em></td> |
| 495 |
</tr> |
| 496 |
<tr class="even"> |
| 497 |
<td><code>path_sanitize("path")</code></td> |
| 498 |
<td><em>No direct equivalent</em></td> |
| 499 |
<td><em>No direct equivalent</em></td> |
| 500 |
</tr> |
| 501 |
</tbody> |
| 502 |
</table> |
| 503 |
</div> |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
<script> |
| 512 |
(function () { |
| 513 |
var script = document.createElement("script"); |
| 514 |
script.type = "text/javascript"; |
| 515 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 516 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 517 |
})(); |
| 518 |
</script> |
| 519 |
|
| 520 |
</body> |
| 521 |
</html> |