Skip to content

Commit 26ca69b

Browse files
committed
usethis::use_air()
1 parent f02e4e7 commit 26ca69b

40 files changed

+1149
-620
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@
2525
^src/README\.md$
2626
^MAINTENANCE\.md$
2727
^pkgdown$
28+
^[\.]?air\.toml$
29+
^\.vscode$

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"Posit.air-vscode"
4+
]
5+
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"[r]": {
3+
"editor.formatOnSave": true,
4+
"editor.defaultFormatter": "Posit.air-vscode"
5+
}
6+
}

R/copy.R

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ file_copy <- function(path, new_path, overwrite = FALSE) {
5757
if (length(new) == 1 && is_directory[[1]]) {
5858
new <- rep(new, length(path))
5959
}
60-
assert("Length of `path` must equal length of `new_path`", length(old) == length(new))
60+
assert(
61+
"Length of `path` must equal length of `new_path`",
62+
length(old) == length(new)
63+
)
6164

6265
new[is_directory] <- path(new[is_directory], basename(old))
6366

@@ -72,7 +75,10 @@ dir_copy <- function(path, new_path, overwrite = FALSE) {
7275
assert_no_missing(path)
7376
assert_no_missing(new_path)
7477
assert("`path` must be a directory", all(is_dir(path)))
75-
assert("Length of `path` must equal length of `new_path`", length(path) == length(new_path))
78+
assert(
79+
"Length of `path` must equal length of `new_path`",
80+
length(path) == length(new_path)
81+
)
7682

7783
for (i in seq_along(path)) {
7884
if (!isTRUE(overwrite) && isTRUE(unname(is_dir(new_path[[i]])))) {
@@ -83,13 +89,31 @@ dir_copy <- function(path, new_path, overwrite = FALSE) {
8389
dirs <- dir_ls(path[[i]], type = "directory", recurse = TRUE, all = TRUE)
8490
dir_create(path(new_path[[i]], path_rel(dirs, path[[i]])))
8591

86-
files <- dir_ls(path[[i]], recurse = TRUE,
87-
type = c("unknown", "file", "FIFO", "socket", "character_device", "block_device"), all = TRUE)
88-
file_copy(files, path(new_path[[i]], path_rel(files, path[[i]])), overwrite = overwrite)
89-
90-
links <- dir_ls(path[[i]], recurse = TRUE,
91-
type = "symlink", all = TRUE)
92-
link_copy(links, path(new_path[[i]], path_rel(links, path[[i]])), overwrite = overwrite)
92+
files <- dir_ls(
93+
path[[i]],
94+
recurse = TRUE,
95+
type = c(
96+
"unknown",
97+
"file",
98+
"FIFO",
99+
"socket",
100+
"character_device",
101+
"block_device"
102+
),
103+
all = TRUE
104+
)
105+
file_copy(
106+
files,
107+
path(new_path[[i]], path_rel(files, path[[i]])),
108+
overwrite = overwrite
109+
)
110+
111+
links <- dir_ls(path[[i]], recurse = TRUE, type = "symlink", all = TRUE)
112+
link_copy(
113+
links,
114+
path(new_path[[i]], path_rel(links, path[[i]])),
115+
overwrite = overwrite
116+
)
93117
}
94118

95119
invisible(path_tidy(new_path))

R/create.R

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#' compared to [dir.create()], `dir_create()` will silently ignore existing
77
#' directories.
88
#'
9-
#' @param path A character vector of one or more paths. For `link_create()`,
9+
#' @param path A character vector of one or more paths. For `link_create()`,
1010
#' this is the target.
1111
#' @param mode If file/directory is created, what mode should it have?
1212
#'
@@ -47,13 +47,23 @@ file_create <- function(path, ..., mode = "u=rw,go=r") {
4747

4848
#' @export
4949
#' @rdname create
50-
dir_create <- function(path, ..., mode = "u=rwx,go=rx", recurse = TRUE, recursive) {
50+
dir_create <- function(
51+
path,
52+
...,
53+
mode = "u=rwx,go=rx",
54+
recurse = TRUE,
55+
recursive
56+
) {
5157
assert_no_missing(path)
5258
assert("`mode` must be of length 1", length(mode) == 1)
5359

5460
if (!missing(recursive)) {
5561
recurse <- recursive
56-
warning("`recursive` is deprecated, please use `recurse` instead", immediate. = TRUE, call. = FALSE)
62+
warning(
63+
"`recursive` is deprecated, please use `recurse` instead",
64+
immediate. = TRUE,
65+
call. = FALSE
66+
)
5767
}
5868

5969
mode <- as_fs_perms(mode)
@@ -88,7 +98,10 @@ dir_create <- function(path, ..., mode = "u=rwx,go=rx", recurse = TRUE, recursiv
8898
link_create <- function(path, new_path, symbolic = TRUE) {
8999
assert_no_missing(path)
90100
assert_no_missing(new_path)
91-
assert("Length of `path` must equal length of `new_path`", length(path) == length(new_path))
101+
assert(
102+
"Length of `path` must equal length of `new_path`",
103+
length(path) == length(new_path)
104+
)
92105

93106
old <- path_expand(path)
94107
new <- path_expand(new_path)

R/delete.R

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,20 @@ dir_delete <- function(path) {
6565
old <- path_expand(path)
6666

6767
dirs <- dir_ls(old, type = "directory", recurse = TRUE, all = TRUE)
68-
files <- dir_ls(old,
69-
type = c("unknown", "file", "symlink", "FIFO", "socket", "character_device", "block_device"),
68+
files <- dir_ls(
69+
old,
70+
type = c(
71+
"unknown",
72+
"file",
73+
"symlink",
74+
"FIFO",
75+
"socket",
76+
"character_device",
77+
"block_device"
78+
),
7079
recurse = TRUE,
71-
all = TRUE)
80+
all = TRUE
81+
)
7282
.Call(fs_unlink_, files)
7383
.Call(fs_rmdir_, rev(c(old, dirs)))
7484

@@ -80,9 +90,7 @@ dir_delete <- function(path) {
8090
link_delete <- function(path) {
8191
assert_no_missing(path)
8292

83-
assert("`path` must be a link",
84-
all(is_link(path))
85-
)
93+
assert("`path` must be a link", all(is_link(path)))
8694

8795
old <- path_expand(path)
8896

R/file.R

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,25 @@ file_info <- function(path, fail = TRUE, follow = FALSE) {
5757
res$change_time <- .POSIXct(res$change_time)
5858
res$birth_time <- .POSIXct(res$birth_time)
5959

60-
important <- c("path", "type", "size", "permissions", "modification_time", "user", "group")
60+
important <- c(
61+
"path",
62+
"type",
63+
"size",
64+
"permissions",
65+
"modification_time",
66+
"user",
67+
"group"
68+
)
6169
res <- res[c(important, setdiff(names(res), important))]
6270

6371
is_symlink <- !is.na(res$type) & res$type == "symlink"
64-
while(follow && any(is_symlink)) {
72+
while (follow && any(is_symlink)) {
6573
lpath <- link_path(path[is_symlink])
66-
lpath <- ifelse(is_absolute_path(lpath), lpath, path(path_dir(path[is_symlink]), lpath))
74+
lpath <- ifelse(
75+
is_absolute_path(lpath),
76+
lpath,
77+
path(path_dir(path[is_symlink]), lpath)
78+
)
6779
res[is_symlink, ] <- file_info(lpath, fail = fail, follow = FALSE)
6880
is_symlink <- !is.na(res$type) & res$type == "symlink"
6981
}
@@ -88,7 +100,8 @@ file_types <- c(
88100
"FIFO" = 3L,
89101
"symlink" = 4L,
90102
"file" = 5L,
91-
"socket" = 6L)
103+
"socket" = 6L
104+
)
92105

93106
#' Change file permissions
94107
#' @template fs
@@ -216,7 +229,10 @@ file_move <- function(path, new_path) {
216229
if (length(new) == 1 && is_directory[[1]]) {
217230
new <- rep(new, length(path))
218231
}
219-
assert("Length of `path` must equal length of `new_path`", length(old) == length(new))
232+
assert(
233+
"Length of `path` must equal length of `new_path`",
234+
length(old) == length(new)
235+
)
220236

221237
new[is_directory] <- path(new[is_directory], basename(old))
222238

@@ -240,7 +256,11 @@ file_move <- function(path, new_path) {
240256
#' file_info("foo")[c("access_time", "modification_time", "change_time", "birth_time")]
241257
#' \dontshow{setwd(.old_wd)}
242258
#' @export
243-
file_touch <- function(path, access_time = Sys.time(), modification_time = access_time) {
259+
file_touch <- function(
260+
path,
261+
access_time = Sys.time(),
262+
modification_time = access_time
263+
) {
244264
assert_no_missing(path)
245265

246266
access_time <- as.POSIXct(access_time)

R/fs_bytes.R

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
units <- c('B' = 1, 'K' = 1024, 'M' = 1024 ^ 2, 'G' = 1024 ^ 3, 'T' = 1024 ^ 4, 'P' = 1024 ^ 5, 'E' = 1024 ^ 6, 'Z' = 1024 ^ 7, 'Y' = 1024 ^ 8)
1+
units <- c(
2+
'B' = 1,
3+
'K' = 1024,
4+
'M' = 1024^2,
5+
'G' = 1024^3,
6+
'T' = 1024^4,
7+
'P' = 1024^5,
8+
'E' = 1024^6,
9+
'Z' = 1024^7,
10+
'Y' = 1024^8
11+
)
212

313
#' Human readable file sizes
414
#'
@@ -40,7 +50,14 @@ as_fs_bytes.default <- function(x = numeric()) {
4050
return(new_fs_bytes(numeric()))
4151
}
4252
x <- as.character(x)
43-
m <- captures(x, regexpr("^(?<size>[[:digit:].]+)\\s*(?<unit>[KMGTPEZY]?)i?[Bb]?$", x, perl = TRUE))
53+
m <- captures(
54+
x,
55+
regexpr(
56+
"^(?<size>[[:digit:].]+)\\s*(?<unit>[KMGTPEZY]?)i?[Bb]?$",
57+
x,
58+
perl = TRUE
59+
)
60+
)
4461
m$unit[m$unit == ""] <- "B"
4562
new_fs_bytes(unname(as.numeric(m$size) * units[m$unit]))
4663
}
@@ -58,12 +75,18 @@ as_fs_bytes.numeric <- function(x) {
5875
# Adapted from https://github.com/gaborcsardi/prettyunits
5976
# Aims to be consistent with ls -lh, so uses 1024 KiB units, 3 or less digits etc.
6077
#' @export
61-
format.fs_bytes <- function(x, scientific = FALSE, digits = 3, drop0trailing = TRUE, ...) {
78+
format.fs_bytes <- function(
79+
x,
80+
scientific = FALSE,
81+
digits = 3,
82+
drop0trailing = TRUE,
83+
...
84+
) {
6285
bytes <- unclass(x)
6386

6487
exponent <- pmin(floor(log(bytes, 1024)), length(units) - 1)
65-
res <- round(bytes / 1024 ^ exponent, 2)
66-
unit <- ifelse (exponent == 0, "", names(units)[exponent + 1])
88+
res <- round(bytes / 1024^exponent, 2)
89+
unit <- ifelse(exponent == 0, "", names(units)[exponent + 1])
6790

6891
## Zero bytes
6992
res[bytes == 0] <- 0
@@ -72,9 +95,15 @@ format.fs_bytes <- function(x, scientific = FALSE, digits = 3, drop0trailing = T
7295
## NA and NaN bytes
7396
res[is.na(bytes)] <- NA_real_
7497
res[is.nan(bytes)] <- NaN
75-
unit[is.na(bytes)] <- "" # Includes NaN as well
98+
unit[is.na(bytes)] <- "" # Includes NaN as well
7699

77-
res <- format(res, scientific = scientific, digits = digits, drop0trailing = drop0trailing, ...)
100+
res <- format(
101+
res,
102+
scientific = scientific,
103+
digits = digits,
104+
drop0trailing = drop0trailing,
105+
...
106+
)
78107

79108
paste0(res, unit)
80109
}
@@ -114,13 +143,16 @@ max.fs_bytes <- function(x, ...) {
114143

115144
#' @export
116145
# Adapted from Ops.numeric_version
117-
Ops.fs_bytes <- function (e1, e2) {
146+
Ops.fs_bytes <- function(e1, e2) {
118147
if (nargs() == 1L) {
119-
stop(sprintf("unary '%s' not defined for \"fs_bytes\" objects", .Generic),
120-
call. = FALSE)
148+
stop(
149+
sprintf("unary '%s' not defined for \"fs_bytes\" objects", .Generic),
150+
call. = FALSE
151+
)
121152
}
122153

123-
boolean <- switch(.Generic,
154+
boolean <- switch(
155+
.Generic,
124156
`+` = TRUE,
125157
`-` = TRUE,
126158
`*` = TRUE,
@@ -132,10 +164,13 @@ Ops.fs_bytes <- function (e1, e2) {
132164
`!=` = TRUE,
133165
`<=` = TRUE,
134166
`>=` = TRUE,
135-
FALSE)
167+
FALSE
168+
)
136169
if (!boolean) {
137-
stop(sprintf("'%s' not defined for \"fs_bytes\" objects", .Generic),
138-
call. = FALSE)
170+
stop(
171+
sprintf("'%s' not defined for \"fs_bytes\" objects", .Generic),
172+
call. = FALSE
173+
)
139174
}
140175
e1 <- as_fs_bytes(e1)
141176
e2 <- as_fs_bytes(e2)

0 commit comments

Comments
 (0)