• Stars
    star
    353
  • Rank 113,682 (Top 3 %)
  • Language
    C
  • License
    Other
  • Created about 6 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Provide cross platform file operations based on libuv.

fs

lifecycle R-CMD-check Codecov test coverage

fs provides a cross-platform, uniform interface to file system operations. It shares the same back-end component as nodejs, the libuv C library, which brings the benefit of extensive real-world use and rigorous cross-platform testing. The name, and some of the interface, is partially inspired by Rustโ€™s fs module.

Installation

You can install the released version of fs from CRAN with:

install.packages("fs")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("r-lib/fs")

Comparison vs base equivalents

fs functions smooth over some of the idiosyncrasies of file handling with base R functions:

  • Vectorization. All fs functions are vectorized, accepting multiple paths as input. Base functions are inconsistently vectorized.

  • Predictable return values that always convey a path. All fs functions return a character vector of paths, a named integer or a logical vector, where the names give the paths. Base return values are more varied: they are often logical or contain error codes which require downstream processing.

  • Explicit failure. If fs operations fail, they throw an error. Base functions tend to generate a warning and a system dependent error code. This makes it easy to miss a failure.

  • UTF-8 all the things. fs functions always convert input paths to UTF-8 and return results as UTF-8. This gives you path encoding consistency across OSes. Base functions rely on the native system encoding.

  • Naming convention. fs functions use a consistent naming convention. Because base Rโ€™s functions were gradually added over time there are a number of different conventions used (e.g.ย path.expand() vs normalizePath(); Sys.chmod() vs file.access()).

Tidy paths

fs functions always return โ€˜tidyโ€™ paths. Tidy paths

  • Always use / to delimit directories
  • never have multiple / or trailing /

Tidy paths are also coloured (if your terminal supports it) based on the file permissions and file type. This colouring can be customized or extended by setting the LS_COLORS environment variable, in the same output format as GNU dircolors.

Usage

fs functions are divided into four main categories:

  • path_ for manipulating and constructing paths
  • file_ for files
  • dir_ for directories
  • link_ for links

Directories and links are special types of files, so file_ functions will generally also work when applied to a directory or link.

library(fs)

# Construct a path to a file with `path()`
path("foo", "bar", letters[1:3], ext = "txt")
#> foo/bar/a.txt foo/bar/b.txt foo/bar/c.txt

# list files in the current directory
dir_ls()
#> DESCRIPTION      LICENSE          LICENSE.md       MAINTENANCE.md   
#> NAMESPACE        NEWS.md          R                README.Rmd       
#> README.md        _pkgdown.yml     cleanup          codecov.yml      
#> cran-comments.md fs.Rproj         inst             man              
#> man-roxygen      revdep           src              tests            
#> vignettes

# create a new directory
tmp <- dir_create(file_temp())
tmp
#> /var/folders/ph/fpcmzfd16rgbbk8mxvy9m2_h0000gn/T/RtmpsYVhMJ/file14eb5f12e1a8

# create new files in that directory
file_create(path(tmp, "my-file.txt"))
dir_ls(tmp)
#> /var/folders/ph/fpcmzfd16rgbbk8mxvy9m2_h0000gn/T/RtmpsYVhMJ/file14eb5f12e1a8/my-file.txt

# remove files from the directory
file_delete(path(tmp, "my-file.txt"))
dir_ls(tmp)
#> character(0)

# remove the directory
dir_delete(tmp)

fs is designed to work well with the pipe, though because it is a minimal-dependency infrastructure package it doesnโ€™t provide the pipe itself. You will need to attach magrittr or similar.

library(magrittr)

paths <- file_temp() %>%
  dir_create() %>%
  path(letters[1:5]) %>%
  file_create()
paths
#> /var/folders/ph/fpcmzfd16rgbbk8mxvy9m2_h0000gn/T/RtmpsYVhMJ/file14eb51c5215df/a
#> /var/folders/ph/fpcmzfd16rgbbk8mxvy9m2_h0000gn/T/RtmpsYVhMJ/file14eb51c5215df/b
#> /var/folders/ph/fpcmzfd16rgbbk8mxvy9m2_h0000gn/T/RtmpsYVhMJ/file14eb51c5215df/c
#> /var/folders/ph/fpcmzfd16rgbbk8mxvy9m2_h0000gn/T/RtmpsYVhMJ/file14eb51c5215df/d
#> /var/folders/ph/fpcmzfd16rgbbk8mxvy9m2_h0000gn/T/RtmpsYVhMJ/file14eb51c5215df/e

paths %>% file_delete()

fs functions also work well in conjunction with other tidyverse packages, like dplyr and purrr.

Some examplesโ€ฆ

suppressMessages(
  library(tidyverse))

Filter files by type, permission and size

dir_info("src", recurse = FALSE) %>%
  filter(type == "file", permissions == "u+r", size > "10KB") %>%
  arrange(desc(size)) %>%
  select(path, permissions, size, modification_time)
#> # A tibble: 11 ร— 4
#>    path          permissions        size modification_time  
#>    <fs::path>    <fs::perms> <fs::bytes> <dttm>             
#>  1 src/fs.so     rwxr-xr-x        492.6K 2023-01-22 21:52:58
#>  2 src/id.o      rw-r--r--        213.5K 2023-01-22 21:52:41
#>  3 src/dir.o     rw-r--r--        102.7K 2023-01-22 21:52:40
#>  4 src/utils.o   rw-r--r--         89.2K 2023-01-22 21:52:41
#>  5 src/path.o    rw-r--r--         81.7K 2023-01-22 21:52:41
#>  6 src/link.o    rw-r--r--         74.2K 2023-01-22 21:52:41
#>  7 src/getmode.o rw-r--r--         65.3K 2023-01-22 21:52:40
#>  8 src/file.o    rw-r--r--         48.5K 2023-01-22 21:52:40
#>  9 src/error.o   rw-r--r--         18.4K 2023-01-22 21:52:40
#> 10 src/init.o    rw-r--r--         17.4K 2023-01-22 21:52:41
#> 11 src/file.cc   rw-r--r--         11.7K 2023-01-22 21:52:29

Tabulate and display folder size.

dir_info("src", recurse = TRUE) %>%
  group_by(directory = path_dir(path)) %>%
  tally(wt = size, sort = TRUE)
#> # A tibble: 14 ร— 2
#>    directory                                n
#>    <chr>                          <fs::bytes>
#>  1 src/libuv-1.44.2                     2.85M
#>  2 src/libuv-1.44.2/src/unix            1.28M
#>  3 src                                  1.22M
#>  4 src/libuv-1.44.2/test                1.05M
#>  5 src/libuv-1.44.2/src/win           742.07K
#>  6 src/libuv-1.44.2/m4                 356.7K
#>  7 src/libuv-1.44.2/src               340.12K
#>  8 src/libuv-1.44.2/include/uv        137.44K
#>  9 src/libuv-1.44.2/img               106.71K
#> 10 src/libuv-1.44.2/include            66.23K
#> 11 src/unix                             60.2K
#> 12 src/bsd                             20.02K
#> 13 src/windows                          4.73K
#> 14 src/libuv-1.44.2/test/fixtures         453

Read a collection of files into one data frame.

dir_ls() returns a named vector, so it can be used directly with purrr::map_df(.id).

# Create separate files for each species
iris %>%
  split(.$Species) %>%
  map(select, -Species) %>%
  iwalk(~ write_tsv(.x, paste0(.y, ".tsv")))

# Show the files
iris_files <- dir_ls(glob = "*.tsv")
iris_files
#> setosa.tsv     versicolor.tsv virginica.tsv

# Read the data into a single table, including the filenames
iris_files %>%
  map_df(read_tsv, .id = "file", col_types = cols(), n_max = 2)
#> # A tibble: 6 ร— 5
#>   file           Sepal.Length Sepal.Width Petal.Length Petal.Width
#>   <chr>                 <dbl>       <dbl>        <dbl>       <dbl>
#> 1 setosa.tsv              5.1         3.5          1.4         0.2
#> 2 setosa.tsv              4.9         3            1.4         0.2
#> 3 versicolor.tsv          7           3.2          4.7         1.4
#> 4 versicolor.tsv          6.4         3.2          4.5         1.5
#> 5 virginica.tsv           6.3         3.3          6           2.5
#> 6 virginica.tsv           5.8         2.7          5.1         1.9

file_delete(iris_files)

Feedback wanted!

We hope fs is a useful tool for both analysis scripts and packages. Please open GitHub issues for any feature requests or bugs.

In particular, we have found non-ASCII filenames in non-English locales on Windows to be especially tricky to reproduce and handle correctly. Feedback from users who use commonly have this situation is greatly appreciated.

Code of Conduct

Please note that the fs project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

More Repositories

1

devtools

Tools to make an R developer's life easier
R
2,336
star
2

lintr

Static Code Analysis for R
R
1,135
star
3

httr

httr: a friendly http package for R
R
975
star
4

actions

GitHub Actions for the R community
JavaScript
868
star
5

testthat

An R ๐Ÿ“ฆ to make testing ๐Ÿ˜€
R
849
star
6

usethis

Set up commonly used ๐Ÿ“ฆ components
R
798
star
7

pkgdown

Generate static html documentation for an R package
R
686
star
8

styler

Non-invasive pretty printing of R code
R
657
star
9

pak

A fresh approach to package installation
C
575
star
10

cli

Tools for making beautiful & useful command line interfaces
R
571
star
11

roxygen2

Generate R package documentation from inline R comments
R
554
star
12

rig

The R Installation Manager
Rust
460
star
13

rlang

Low-level API for programming with R
R
454
star
14

progress

Progress bar in your R terminal
R
447
star
15

R6

Encapsulated object-oriented programming for R
R
393
star
16

here

A simpler way to find your files
R
387
star
17

scales

Tools for ggplot2 scales
R
373
star
18

covr

Test coverage reports for R
R
328
star
19

rex

Friendly regular expressions for R.
R
325
star
20

crayon

๐Ÿ–๏ธ R package for colored terminal output โ€” now superseded by cli
R
321
star
21

memoise

Easy memoisation for R
R
310
star
22

remotes

Install R packages from GitHub, GitLab, Bitbucket, git, svn repositories, URLs
R
309
star
23

lobstr

Understanding complex R objects with tools similar to str()
R
294
star
24

callr

Call R from R
R
281
star
25

vctrs

Generic programming with typed R vectors
C
272
star
26

waldo

Find differences between R objects
R
272
star
27

slider

Sliding Window Functions
R
267
star
28

zeallot

Variable assignment with zeal! (or multiple, unpacking, and destructuring assignment in R)
R
245
star
29

conflicted

An alternative conflict resolution strategy for R
R
242
star
30

bench

High Precision Timing of R Expressions
R
237
star
31

gmailr

Access the Gmail RESTful API from R.
R
234
star
32

processx

Execute and Control Subprocesses from R
R
225
star
33

xml2

Bindings to libxml2
R
212
star
34

asciicast

Turn R scripts into terminal screencasts
R
211
star
35

gh

Minimalistic GitHub API client in R
R
210
star
36

httr2

Make HTTP requests and process their responses. A modern reimagining of httr.
R
206
star
37

cpp11

cpp11 helps you to interact with R objects using C++ code.
C++
187
star
38

keyring

๐Ÿ” Access the system credential store from R
R
185
star
39

vdiffr

Visual regression testing and graphical diffing with testthat
C++
177
star
40

svglite

A lightweight svg graphics device for R
C++
177
star
41

pillar

Format columns with colour
R
173
star
42

ragg

Graphic Devices Based on AGG
C++
169
star
43

ymlthis

write YAML for R Markdown, bookdown, blogdown, and more
R
163
star
44

hugodown

Make websites with hugo and RMarkdown
R
163
star
45

withr

Methods For Temporarily Modifying Global State
R
162
star
46

coro

Coroutines for R
R
146
star
47

rprojroot

Finding files in project subdirectories
R
146
star
48

debugme

Easy and efficient debugging for R packages
R
144
star
49

available

Check if a package name is available to use
R
141
star
50

ellipsis

Tools for Working with ...
R
138
star
51

archive

R bindings to libarchive, supporting a large variety of archive formats
C++
138
star
52

gert

Simple git client for R
C
136
star
53

later

Schedule an R function or formula to run after a specified period of time.
C++
132
star
54

rray

Simple Arrays
R
130
star
55

isoband

isoband: An R package to generate contour lines and polygons.
C++
130
star
56

fastmap

Fast map implementation for R
C++
128
star
57

prettyunits

Pretty, human readable formatting of quantities
JavaScript
126
star
58

tidyselect

A backend for functions taking tidyverse selections
R
122
star
59

desc

Manipulate DESCRIPTION files
R
120
star
60

gargle

Infrastructure for calling Google APIs from R, including auth
R
112
star
61

rcmdcheck

Run R CMD check from R and collect the results
R
110
star
62

evaluate

A version of eval for R that returns more information about what happened
R
107
star
63

prettycode

Syntax highlight R code in the terminal
R
100
star
64

mockery

A mocking library for R.
R
100
star
65

sloop

S language OOP โ›ต๏ธ
R
98
star
66

pkgdepends

R Package Dependency Resolution
R
93
star
67

revdepcheck

R package reverse dependency checking
R
93
star
68

clock

A Date-Time Library for R
R
93
star
69

lifecycle

Manage the life cycle of your exported functions and arguments
R
91
star
70

systemfonts

System Native Font Handling in R
C++
90
star
71

gtable

The layout packages that powers ggplot2
R
85
star
72

askpass

Password Entry for R, Git, and SSH
R
83
star
73

rappdirs

Find OS-specific directories to store data, caches, and logs. A port of python's AppDirs
R
81
star
74

zip

Platform independent zip compression via miniz
C
81
star
75

commonmark

High Performance CommonMark and Github Markdown Rendering in R
C
81
star
76

downlit

Syntax Highlighting and Automatic Linking
R
80
star
77

clisymbols

Unicode symbols for CLI applications, with fallbacks
R
74
star
78

tree-sitter-r

C
74
star
79

ps

R package to query, list, manipulate system processes
C
72
star
80

sessioninfo

Print Session Information
R
72
star
81

pkgapi

Create a map of functions for an R package - WORK IN PROGRESS!
R
69
star
82

credentials

Tools for Managing SSH and Git Credentials
R
69
star
83

roxygen2md

Convert elements of roxygen documentation to markdown
R
69
star
84

sodium

R bindings to libsodium
R
68
star
85

backports

Reimplementations of Functions Introduced Since R-3.0.0
R
65
star
86

pkgbuild

Find tools needed to build R packages
R
65
star
87

cliapp

Rich Command Line Applications
R
62
star
88

webfakes

Fake web apps for HTTP testing R packages
C
61
star
89

generics

Common generic methods
R
60
star
90

diffviewer

HTML widget to visually compare files
JavaScript
57
star
91

liteq

Serverless R message queue using SQLite
R
55
star
92

pkgload

Simulate installing and loading a package
R
55
star
93

cachem

Key-value caches for R
R
53
star
94

carrier

Create standalone functions for remote execution
R
49
star
95

brio

Basic R Input Output
R
49
star
96

jose

Javascript Object Signing and Encryption for R
R
47
star
97

urlchecker

Run CRAN URL checks from older versions of R
R
46
star
98

pkgconfig

Private configuration for R packages
R
40
star
99

filelock

Cross platform file locking in R
R
39
star
100

pkginstall

Provides a replacement for `utils::install.packages()`
R
35
star