Skip to content

Commit 478cefe

Browse files
lorenzwaltherthadley
authored andcommitted
styling comprehensively with styler (#393)
* styling with strict = FALSE and scope = "tokens" * styling with styler#318. strict = TRUE, scope = "tokens"). This version handles line breaks in function calls according to tidyverse/style#39.
1 parent 5bd376e commit 478cefe

File tree

10 files changed

+57
-38
lines changed

10 files changed

+57
-38
lines changed

R/expand.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ crossing <- function(...) {
138138
"Each element must be either an atomic vector or a data frame.
139139
Problems: {problems}."
140140
))
141-
142141
}
143142

144143
# turn each atomic vector into single column data frame

R/extract.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ extract.data.frame <- function(data, col, into, regex = "([[:alnum:]]+)",
8484
#' @inheritParams extract
8585
#' @export
8686
extract_ <- function(data, col, into, regex = "([[:alnum:]]+)", remove = TRUE,
87-
convert = FALSE, ...) {
87+
convert = FALSE, ...) {
8888
UseMethod("extract_")
8989
}
9090
#' @export

R/gather.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ gather.data.frame <- function(data, key = "key", value = "value", ...,
132132
args <- normalize_melt_arguments(data, gather_idx, factorsAsStrings = TRUE)
133133
valueAsFactor <- "factor" %in% class(args$attr_template)
134134

135-
out <- melt_dataframe(data,
135+
out <- melt_dataframe(
136+
data,
136137
id_idx - 1L,
137138
gather_idx - 1L,
138139
as.character(key_var),
@@ -159,7 +160,6 @@ gather.data.frame <- function(data, key = "key", value = "value", ...,
159160

160161
## Get the attributes if common, NULL if not.
161162
normalize_melt_arguments <- function(data, measure.ind, factorsAsStrings) {
162-
163163
measure.attributes <- map(measure.ind, function(i) {
164164
attributes(data[[i]])
165165
})
@@ -172,7 +172,8 @@ normalize_melt_arguments <- function(data, measure.ind, factorsAsStrings) {
172172
} else {
173173
warn(glue(
174174
"attributes are not identical across measure variables;
175-
they will be dropped"))
175+
they will be dropped"
176+
))
176177
attr_template <- NULL
177178
}
178179

R/replace_na.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ replace_na.data.frame <- function(data, replace = list(), ...) {
4343

4444
check_replacement <- function(x, var) {
4545
n <- length(x)
46-
if (n == 1)
46+
if (n == 1) {
4747
return()
48+
}
4849

4950
abort(glue("Replacement for `{var}` is length {n}, not length 1"))
5051
}

R/unnest.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ unnest.default <- function(data, ..., .drop = NA, .id = NULL, .sep = NULL, .pres
8484
#' @export
8585
unnest.data.frame <- function(data, ..., .drop = NA, .id = NULL,
8686
.sep = NULL, .preserve = NULL) {
87-
8887
preserve <- tidyselect::vars_select(names(data), !!! enquo(.preserve))
8988
quos <- quos(...)
9089
if (is_empty(quos)) {
@@ -122,13 +121,16 @@ unnest.data.frame <- function(data, ..., .drop = NA, .id = NULL,
122121

123122
unnested_dataframe <- map(nest_types$dataframe %||% list(), dplyr::bind_rows, .id = .id)
124123
if (!is_null(.sep)) {
125-
unnested_dataframe <- imap(unnested_dataframe,
124+
unnested_dataframe <- imap(
125+
unnested_dataframe,
126126
function(df, name) {
127127
set_names(df, paste(name, names(df), sep = .sep))
128-
})
128+
}
129+
)
129130
}
130-
if (length(unnested_dataframe) > 0)
131+
if (length(unnested_dataframe) > 0) {
131132
unnested_dataframe <- dplyr::bind_cols(unnested_dataframe)
133+
}
132134

133135
# Keep list columns by default, only if the rows aren't expanded
134136
if (identical(.drop, NA)) {

R/utils.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ extract_numeric <- function(x) {
5858
NULL
5959

6060
list_indices <- function(x, max = 20) {
61-
if (length(x) > max)
61+
if (length(x) > max) {
6262
x <- c(x[seq_len(max)], "...")
63+
}
6364

6465
paste(x, collapse = ", ")
6566
}

tests/testthat/test-fill.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ test_that("missings filled down for each atomic vector", {
3434
dbl = c(1, NA),
3535
chr = c("a", NA),
3636
lst = list(1:5, NULL)
37-
3837
)
3938

4039
out <- fill(df, tidyselect::everything())

tests/testthat/test-gather.R

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ test_that("key preserves column ordering when factor_key = TRUE", {
4646

4747
test_that("preserve class of input", {
4848
dat <- data.frame(x = 1:2)
49-
dat %>% as_tibble %>% gather %>% expect_is("tbl_df")
49+
dat %>% as_tibble() %>% gather() %>% expect_is("tbl_df")
5050
})
5151

5252
test_that("additional inputs control which columns to gather", {
@@ -95,8 +95,10 @@ test_that("factors coerced to characters, not integers", {
9595
v2 = factor(letters[1:3])
9696
)
9797

98-
expect_warning(out <- gather(df, k, v),
99-
"attributes are not identical across measure variables")
98+
expect_warning(
99+
out <- gather(df, k, v),
100+
"attributes are not identical across measure variables"
101+
)
100102

101103
expect_equal(out$v, c(1:3, letters[1:3]))
102104
})
@@ -121,8 +123,10 @@ test_that("varying attributes are dropped with a warning", {
121123
date1 = as.POSIXct(Sys.Date()),
122124
date2 = Sys.Date() + 10
123125
)
124-
expect_warning(gather(df, k, v),
125-
"attributes are not identical across measure variables")
126+
expect_warning(
127+
gather(df, k, v),
128+
"attributes are not identical across measure variables"
129+
)
126130
})
127131

128132
test_that("gather preserves OBJECT bit on e.g. POSIXct", {

tests/testthat/test-spread.R

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ test_that("drop = FALSE spread all levels including NA (#254)", {
7474
df <- data.frame(
7575
x = factor(c("a", "b", "c", NA), levels = l),
7676
y = c("a", "b", "c", "d"),
77-
z = c("a", "b", "a", "b"))
77+
z = c("a", "b", "a", "b")
78+
)
7879
out <- df %>% spread(x, y, drop = FALSE)
7980
expect_equal(nrow(out), 2)
8081
expect_equal(ncol(out), 6)
@@ -92,10 +93,12 @@ test_that("preserve class of input", {
9293
})
9394

9495
test_that("dates are spread into columns (#62)", {
95-
df <- data.frame(id = c("a", "a", "b", "b"),
96-
key = c("begin", "end", "begin", "end"),
97-
date = Sys.Date() + 0:3,
98-
stringsAsFactors = FALSE)
96+
df <- data.frame(
97+
id = c("a", "a", "b", "b"),
98+
key = c("begin", "end", "begin", "end"),
99+
date = Sys.Date() + 0:3,
100+
stringsAsFactors = FALSE
101+
)
99102
out <- spread(df, key, date)
100103
expect_identical(names(out), c("id", "begin", "end"))
101104
expect_is(out$begin, "Date")
@@ -106,40 +109,50 @@ test_that("spread can produce mixed variable types (#118)", {
106109
df <- data.frame(
107110
row = rep(1:2, 3),
108111
column = rep(1:3, each = 2),
109-
cell_contents = as.character(c(rep("Argentina", 2),
110-
62.485, 64.399,
111-
1952, 1957)),
112+
cell_contents = as.character(c(
113+
rep("Argentina", 2),
114+
62.485, 64.399,
115+
1952, 1957
116+
)),
112117
stringsAsFactors = FALSE
113118
)
114119
out <- spread(df, column, cell_contents, convert = TRUE)
115-
expect_equivalent(vapply(out, class, ""),
116-
c("integer", "character", "numeric", "integer"))
120+
expect_equivalent(
121+
vapply(out, class, ""),
122+
c("integer", "character", "numeric", "integer")
123+
)
117124
})
118125

119126
test_that("factors can be used with convert = TRUE to produce mixed types", {
120-
df <- data.frame(row = c(1, 2, 1, 2, 1, 2),
121-
column = c("f", "f", "g", "g", "h", "h"),
122-
contents = c("aa", "bb", "1", "2", "TRUE", "FALSE"),
123-
stringsAsFactors = FALSE)
127+
df <- data.frame(
128+
row = c(1, 2, 1, 2, 1, 2),
129+
column = c("f", "f", "g", "g", "h", "h"),
130+
contents = c("aa", "bb", "1", "2", "TRUE", "FALSE"),
131+
stringsAsFactors = FALSE
132+
)
124133
out <- df %>% spread(column, contents, convert = TRUE)
125134
expect_is(out$f, "character")
126135
expect_is(out$g, "integer")
127136
expect_is(out$h, "logical")
128137
})
129138

130139
test_that("dates can be used with convert = TRUE", {
131-
df <- data.frame(id = c("a", "a", "b", "b"),
132-
key = c("begin", "end", "begin", "end"),
133-
date = Sys.Date() + 0:3,
134-
stringsAsFactors = FALSE)
140+
df <- data.frame(
141+
id = c("a", "a", "b", "b"),
142+
key = c("begin", "end", "begin", "end"),
143+
date = Sys.Date() + 0:3,
144+
stringsAsFactors = FALSE
145+
)
135146
out <- spread(df, key, date, convert = TRUE)
136147
expect_is(out$begin, "character")
137148
expect_is(out$end, "character")
138149
})
139150

140151
test_that("vars that are all NA are logical if convert = TRUE (#118)", {
141-
df <- data.frame(row = c(1, 2, 1, 2), column = c("f", "f", "g", "g"),
142-
contents = c("aa", "bb", NA, NA), stringsAsFactors = FALSE)
152+
df <- data.frame(
153+
row = c(1, 2, 1, 2), column = c("f", "f", "g", "g"),
154+
contents = c("aa", "bb", NA, NA), stringsAsFactors = FALSE
155+
)
143156
out <- df %>% spread(column, contents, convert = TRUE)
144157
expect_is(out$g, "logical")
145158
})

tests/testthat/test-unnest.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ test_that("unnest respects .drop_lists", {
152152

153153
expect_equal(df %>% unnest(y, .drop = TRUE) %>% names(), c("x", "y"))
154154
expect_equal(df %>% unnest(z, .drop = FALSE) %>% names(), c("x", "y", "z"))
155-
156155
})
157156

158157
test_that("grouping is preserved", {

0 commit comments

Comments
 (0)