Skip to content

Commit 7056922

Browse files
committed
Merge branch 'r-0.0-9' into production
2 parents 68e3f29 + 679d0e0 commit 7056922

File tree

251 files changed

+5200
-841
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

251 files changed

+5200
-841
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
^tic\.R$
99
^\.travis\.yml$
1010
^docs$
11+
^_pkgdown\.yml$

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.Rhistory
33
.RData
44
inst/doc
5+
docs

API

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
## Exported functions
44

5-
create_style_guide(filler = create_filler, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE)
6-
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), recursive = TRUE)
5+
create_style_guide(initialize = initialize_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention())
6+
regex_none()
7+
specify_math_token_spacing(zero = NULL, one = c("'+'", "'-'", "'*'", "'/'", "'^'"))
8+
specify_reindention(regex_pattern = regex_none(), indention = 0, comments_only = TRUE)
9+
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), recursive = TRUE, exclude_files = NULL)
710
style_file(path, ..., style = tidyverse_style, transformers = style(...))
8-
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...))
11+
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), exclude_files = "R/RcppExports.R")
912
style_text(text, ..., style = tidyverse_style, transformers = style(...))
10-
tidyverse_style(scope = "tokens", strict = TRUE, indent_by = 2, start_comments_with_one_space = FALSE)
13+
tidyverse_math_token_spacing()
14+
tidyverse_reindention()
15+
tidyverse_style(scope = "tokens", strict = TRUE, indent_by = 2, start_comments_with_one_space = FALSE, reindention = tidyverse_reindention(), math_token_spacing = tidyverse_math_token_spacing())

DESCRIPTION

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
11
Package: styler
22
Title: Non-invasive Pretty Printing of R code
3-
Version: 0.0-8
3+
Version: 0.0-9
44
Authors@R: c(person("Kirill", "Müller", role = c("aut"), email = "[email protected]"),
55
person("Lorenz", "Walthert", role = c("cre", "aut"), email = "[email protected]"))
66
Description:
77
Pretty-prints R code without changing the user's formatting intent.
88
Imports:
9-
dplyr,
109
purrr,
1110
rlang,
1211
rprojroot,
1312
tibble,
14-
tidyr,
1513
utf8,
16-
withr
14+
withr,
15+
rematch2,
16+
magrittr
1717
Suggests:
1818
data.tree,
19+
dplyr,
1920
here,
2021
knitr,
22+
mockr,
2123
rmarkdown,
2224
rstudioapi,
23-
testthat,
24-
pkgload
25+
testthat
2526
Remotes:
26-
r-lib/pkgload,
2727
krlmlr/utf8
2828
License: GPL-3
2929
Encoding: UTF-8
3030
LazyData: true
31-
Date: 2017-08-24
31+
Date: 2017-10-23
3232
BugReports: https://github.com/krlmlr/styler/issues
3333
URL: https://github.com/krlmlr/styler, http://krlmlr.github.io/styler
3434
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "collate", "pkgapi::api_roclet"))
35-
RoxygenNote: 6.0.1.9000
35+
RoxygenNote: 6.0.1
3636
VignetteBuilder: knitr
3737
Collate:
38+
'addins.R'
39+
'compat-tidyr.R'
40+
'dplyr.R'
41+
'expr-is.R'
42+
'initialize.R'
3843
'modify_pd.R'
3944
'nested.R'
4045
'nested_to_tree.R'
41-
'parsed.R'
46+
'parse.R'
4247
'reindent.R'
43-
'token.R'
48+
'token-define.R'
4449
'relevel.R'
4550
'rules-line_break.R'
4651
'rules-other.R'
@@ -49,9 +54,12 @@ Collate:
4954
'serialize.R'
5055
'serialized_tests.R'
5156
'style_guides.R'
57+
'style_rmd.R'
5258
'styler.R'
59+
'token-create.R'
5360
'transform.R'
5461
'unindent.R'
5562
'utils.R'
63+
'vertical.R'
5664
'visit.R'
5765
'ws.R'

NAMESPACE

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(print,vertical)
34
export(create_style_guide)
5+
export(regex_none)
6+
export(specify_math_token_spacing)
7+
export(specify_reindention)
48
export(style_dir)
59
export(style_file)
610
export(style_pkg)
711
export(style_text)
12+
export(tidyverse_math_token_spacing)
13+
export(tidyverse_reindention)
814
export(tidyverse_style)
9-
import(dplyr)
1015
import(tibble)
11-
import(tidyr)
16+
importFrom(magrittr,"%>%")
1217
importFrom(purrr,flatten)
1318
importFrom(purrr,flatten_chr)
19+
importFrom(purrr,flatten_int)
1420
importFrom(purrr,map)
1521
importFrom(purrr,map2)
1622
importFrom(purrr,map_chr)

NEWS.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
1+
## styler 0.0-9 (2017-10-23)
2+
3+
- Hotfix: utf8 should not be verbose (#245).
4+
- Allow styling of Rmd files(#233).
5+
- Remove duplicate @family (#244).
6+
- Fixing token insertion (#242).
7+
- Capitalize Addin titles (#241).
8+
- Explicit `NULL` creation to make styler compatible with R3.2.0 (#237).
9+
- Improve vignettes (#232).
10+
- Allow exclusion of files with `style_pkg()` and `style_dir()`.
11+
- Correct styling with long strings (#230).
12+
- Add tools for re-indenting tokens (#217).
13+
- Math token spacing (#221).
14+
- Remove outdated line and col information (#218).
15+
- Empty input for styling does not cause an error (#227, #228).
16+
- Tools to insert tokens + application on `if`-`else` clauses (#212).
17+
- Improve example in documentation (#222).
18+
- Fix spacing around in (#214).
19+
- Maintenance: renaming functions / files, extend helper, documentation, if_else etc. (#204).
20+
- Disallow line break after ( for function calls (#210).
21+
- Preserve space between `!` and bang (#209).
22+
- Simplify RStudio Addin (#208, #211).
23+
- Indention based on square brackets (#207).
24+
- Add vignette on introducing styler (#203).
25+
- Indent function declaration without curly braces correctly (#202).
26+
- Fix indention in if-else statement (#200).
27+
- Sorting key (#196).
28+
- Use safe sequences (#197).
29+
- Fix space between two commas (#195).
30+
- Keep single-line pipes on one line (#74).
31+
- Remove tidyr and dplyr dependency (#182, #183, @jimhester).
32+
- Fix parsing inconsistency (#188).
33+
- Substitute create filler (#190).
34+
- Introducing class vertical (#189).
35+
- Adapt line break rules (#172).
36+
- Fix `R CMD check` (#185).
37+
- Force argument evaluation for proper error handling (#179).
38+
- Add nonstrict version of set_space_before_comment (#174).
39+
- Add installation instructions to README (#175).
40+
- Addin to style highlighted region (#143).
41+
- Improve spelling (#168).
42+
- Add coverage badge
43+
- Change badge from WIP to active
44+
- Add the number of files to message (#166).
45+
- Improve documentation (#164).
46+
- Add informative messages while styling files (#165).
47+
- More examples in help file (#161).
48+
- No line breaks after pipe if comment is next token (#160).
49+
- Fixing spacing around `!` (non-bang-bang) (#157).
50+
- Finalize function documentation (#154).
51+
- Review vignette (#158).
52+
- Update bang-bang rule (#156).
53+
54+
155
## styler 0.0-8 (2017-08-24)
256

357
- Vignette on customizing styler (#145).

R/addins.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' Style the active file
2+
#'
3+
#' Helper function for RStudio Addin.
4+
style_active_file <- function() {
5+
context <- get_rstudio_context()
6+
style_file(context$path, style = tidyverse_style)
7+
}
8+
9+
10+
#' Style the highlighted region
11+
#'
12+
#' Helper function for RStudio Addin. This function is complicated because of
13+
#' one thing: You can highlight also just parts of lines.
14+
#' @importFrom rlang seq2
15+
style_active_region <- function() {
16+
context <- get_rstudio_context()
17+
text <- context$selection[[1]]$text
18+
if (all(nchar(text) == 0)) stop("No code selected")
19+
out <- style_text(text)
20+
rstudioapi::modifyRange(
21+
context$selection[[1]]$range, paste0(out, collapse = "\n"), id = context$id
22+
)
23+
}
24+
25+
get_rstudio_context <- function() {
26+
rstudioapi::getActiveDocumentContext()
27+
}

R/compat-tidyr.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
nest_ <- function(data, key_col, nest_cols = character()) {
2+
key_column <- setdiff(names(data), nest_cols)
3+
key_data <- data[[key_column]]
4+
key_levels <- unique(key_data)
5+
key_factor <- factor(key_data, levels = key_levels)
6+
res <- list()
7+
res[[key_column]] <- key_levels
8+
res[[key_col]] <- split(data[, nest_cols], key_factor)
9+
as_tibble(res)
10+
}

R/dplyr.R

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
lag <- function(x, n = 1L, default = NA, ...) {
2+
if (n == 0) {
3+
return(x)
4+
}
5+
xlen <- length(x)
6+
n <- pmin(n, xlen)
7+
out <- c(rep(default, n), x[seq_len(xlen - n)])
8+
attributes(out) <- attributes(x)
9+
out
10+
}
11+
12+
lead <- function(x, n = 1L, default = NA, ...) {
13+
if (n == 0) {
14+
return(x)
15+
}
16+
xlen <- length(x)
17+
n <- pmin(n, xlen)
18+
out <- c(x[-seq_len(n)], rep(default, n))
19+
attributes(out) <- attributes(x)
20+
out
21+
}
22+
23+
arrange <- function(.data, ...) {
24+
stopifnot(is.data.frame(.data))
25+
ord <- eval(substitute(order(...)), .data, parent.frame())
26+
if (length(ord) != nrow(.data)) {
27+
stop("Length of ordering vectors don't match data frame size",
28+
call. = FALSE)
29+
}
30+
.data[ord, , drop = FALSE]
31+
}
32+
33+
if_else <- function(condition, true, false, missing = NULL) {
34+
stopifnot(length(condition) == length(true))
35+
stopifnot(length(condition) == length(false))
36+
if (!is.null(missing)) stop("missing arg not yet implemented")
37+
ifelse(condition, true, false)
38+
}
39+
40+
bind_rows <- function(x, y = NULL, ...) {
41+
if (is.null(x) && is.null(y)) {
42+
return(tibble())
43+
}
44+
if (is.null(x)) {
45+
if (inherits(y, "data.frame")) {
46+
return(y)
47+
}
48+
return(do.call(rbind.data.frame, x))
49+
}
50+
if (is.null(y)) {
51+
if (inherits(x, "data.frame")) {
52+
return(x)
53+
}
54+
return(do.call(rbind.data.frame, x))
55+
}
56+
if (NCOL(x) != NCOL(y)) {
57+
for (nme in setdiff(names(x), names(y))) {
58+
y[[nme]] <- NA
59+
}
60+
}
61+
bind_rows(rbind.data.frame(x, y), ...)
62+
}
63+
64+
filter <- function(.data, ...) {
65+
subset(.data, ...)
66+
}
67+
68+
left_join <- function(x, y, by, ...) {
69+
if (rlang::is_named(by)) {
70+
by_x <- names(by)
71+
by_y <- unname(by)
72+
} else {
73+
by_x <- by_y <- by
74+
}
75+
res <- as_tibble(merge(x, y, by.x = by_x, by.y = by_y, all.x = TRUE, ...))
76+
res <- arrange(res, line1, col1, line2, col2, parent)
77+
78+
# dplyr::left_join set unknown list columns to NULL, merge sets them
79+
# to NA
80+
if (exists("child", res) && any(is.na(res$child))) {
81+
res$child[is.na(res$child)] <- list(NULL)
82+
}
83+
res
84+
}
85+
86+
nth <- function (x, n, order_by = NULL, default = x[NA_real_]) {
87+
stopifnot(length(n) == 1, is.numeric(n))
88+
n <- trunc(n)
89+
if (n == 0 || n > length(x) || n < -length(x)) {
90+
return(default)
91+
}
92+
if (n < 0) {
93+
n <- length(x) + n + 1
94+
}
95+
if (is.null(order_by)) {
96+
x[[n]]
97+
}
98+
else {
99+
x[[order(order_by)[[n]]]]
100+
}
101+
}
102+
103+
104+
last <- function (x, order_by = NULL, default = x[NA_real_]) {
105+
nth(x, -1L, order_by = order_by, default = default)
106+
}
107+
108+
slice <- function(.data, ...) {
109+
.data[c(...), , drop = FALSE]
110+
}

R/expr-is.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#' Check whether a parse table corresponds to a a certain expression
2+
#'
3+
#' @param pd A parse table.
4+
#' @name pd_is
5+
NULL
6+
7+
#' @describeIn pd_is Checks whether `pd` contains an expression wrapped in
8+
#' curly brackets.
9+
is_curly_expr <- function(pd) {
10+
if (is.null(pd)) return(FALSE)
11+
pd$token[1] == "'{'"
12+
}
13+
14+
is_subset_expr <- function(pd) {
15+
if (is.null(pd) || nrow(pd) == 1) return(FALSE)
16+
pd$token[2] == "'['"
17+
}
18+
19+
#' @describeIn pd_is Checks whether `pd` is a function call.
20+
is_function_call <- function(pd) {
21+
if (is.null(pd)) return(FALSE)
22+
if (is.na(pd$token_before[2])) return(FALSE)
23+
pd$token_before[2] == "SYMBOL_FUNCTION_CALL"
24+
}

0 commit comments

Comments
 (0)