Debian Codegraph @

inst/doc/function-comparisons.Rmd

raw
1 ---
2 title: "Comparison of fs functions, base R, and shell commands"
3 output: rmarkdown::html_vignette
4 vignette: >
5 %\VignetteIndexEntry{base vs fs}
6 %\VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
8 ---
9
10 ```{r}
11 #| label: setup
12 #| include: false
13 knitr::opts_chunk$set(
14 collapse = TRUE,
15 comment = "#>"
16 )
17 ```
18
19 Most of the functionality is **fs** can be approximated with functions in **base R**
20 or in a command line shell. The table at the end of this vignette can be used
21 as a translation aid between these three methods.
22
23 **fs** functions smooth over some of the idiosyncrasies of file handling with
24 base R functions:
25
26 * Vectorization. All **fs** functions are vectorized, accepting multiple paths
27 as input. Base functions are inconsistently vectorized.
28
29 * Predictable return values that always convey a path. All **fs** functions
30 return a character vector of paths, a named integer or a logical vector, where
31 the names give the paths. Base return values are more varied: they are often
32 logical or contain error codes which require downstream processing.
33
34 * Explicit failure. If **fs** operations fail, they throw an error. Base
35 functions tend to generate a warning and a system dependent error code. This
36 makes it easy to miss a failure.
37
38 * UTF-8 all the things. **fs** functions always convert input paths to UTF-8 and
39 return results as UTF-8. This gives you path encoding consistency across OSes.
40 Base functions rely on the native system encoding.
41
42 * Naming convention. **fs** functions use a consistent naming convention.
43 Because base R's functions were gradually added over time there are a number
44 of different conventions used (e.g. `path.expand()` vs `normalizePath()`;
45 `Sys.chmod()` vs `file.access()`).
46
47 ## Directory functions
48
49 | fs | base | shell |
50 | --- | --- | --- |
51 | `dir_ls("path")` | `list.files("path")` | `ls path` |
52 | `dir_info("path")` | `file.info(list.files("path", full.names = TRUE))` | `ls -al path` |
53 | `dir_copy("path", "new-path")` | `dir.create("new-path"); file.copy("path", "new-path", recursive=TRUE)` | `cp path new-path` |
54 | `dir_create("path")` | `dir.create("path")` | `mkdir path` |
55 | `dir_delete("path")` | `unlink("path", recursive = TRUE)` | `rm -rf path` |
56 | `dir_exists("path")` | `dir.exists("path")` | `if [ -d "path" ]; then ... ; fi` |
57 | ~~`dir_move()`~~ (see `file_move`) | `file.rename("path", "new-path")` | `mv path new-path` |
58 | `dir_map("path", fun)` | *No direct equivalent* | `for file in $(ls path); do ...; done` |
59 | `dir_tree("path")` | *No direct equivalent* | `tree path` |
60
61
62 ## File functions
63
64 | fs | base | shell |
65 | --- | --- | --- |
66 | `file_chmod("path", "mode")` | `Sys.chmod("path", "mode")` | `chmod mode path` |
67 | `file_chown("path", "user_id", "group_id")` | *No direct equivalent* | `chown options path ` |
68 | `file_copy("path", "new-path")` | `file.copy("path", "new-path")` | `cp path new-path` |
69 | `file_create("new-path")` | `file.create("new-path")` | `touch new-path` |
70 | `file_delete("path")` | `unlink("path")` | `rm path` |
71 | `file_exists("path")` | `file.exists("path")` | `if [ -f "path" ]; then ... ; fi` |
72 | `file_info("path")` | `file.info("path")` | `ls -al path` |
73 | `file_move("path", "new-path")` | `file.rename("path", "new-path")` | `mv path new-path` |
74 | `file_show("path")` | `browseURL("path")` | `open path` |
75 | `file_touch()` | *No direct equivalent* | `touch path` |
76 | `file_temp()` | `tempfile()` | `mktemp` |
77 | *No direct equivalent* | `file_test()` | `if [ -d "path" ]; then ...; fi` |
78
79 ## Path functions
80
81 | fs | base | shell |
82 | --- | --- | --- |
83 | `path("top_dir", "nested_dir", "file", ext = "ext")` | `file.path("top_dir", "nested_dir", "file.ext")` | `top_dir/nested_dir/file.ext` |
84 | `path_temp()`, `path_temp("path")` | `tempdir()`, `file.path(tempdir(), "path")` | `mktemp -d` |
85 | `path_expand("~/path")` | `path.expand()` | `realpath -m -s ~/path` |
86 | `path_dir("path")` | `dirname("path")` | `dirname path` |
87 | `path_file("path")` | `basename("path")` | `basename path` |
88 | `path_home()` | `path.expand("~")` | `$HOME` |
89 | `path_package("pkgname", "dir", "file")` | `system.file("dir", "file", package = "pkgname")` | *No direct equivalent* |
90 | `path_norm("path")` | `normalizePath()` | `realpath` |
91 | `path_real("path")` | `normalizePath(mustWork = TRUE)` | `realpath` |
92 | `path_rel("path/foo", "path/bar")` | *No direct equivalent* | *No direct equivalent* |
93 | `path_common(c("path/foo", "path/bar", "path/baz"))` | *No direct equivalent* | *No direct equivalent* |
94 | `path_ext_remove("path")` | `sub("\\.[a-zA-Z0-9]*$", "", "path")` | *No direct equivalent* |
95 | `path_ext_set("path", "new_ext")` | `sub("\\.[a-zA-Z0-9]*$", "new_ext", "path")` | *No direct equivalent* |
96 | `path_sanitize("path")` | *No direct equivalent* | *No direct equivalent* |