Skip to content

Commit 778c821

Browse files
committed
using flint package to establish better lints and fixes
1 parent 42e2dd5 commit 778c821

Some content is hidden

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

77 files changed

+2337
-190
lines changed

.Rbuildignore

+4
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ LICENSE
5050
^__autograph*
5151
^testlog
5252
^data-raw$
53+
54+
55+
# flint files
56+
^flint$

R/checkers.R

+6-8
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,15 @@ check_tf_version <- function(alert = c("none",
3838
correct_tfp = have_tfp()
3939
)
4040

41-
if ((all(requirements_valid))) {
42-
43-
if (!greta_stash$python_has_been_initialised) {
41+
py_not_init <- !greta_stash$python_has_been_initialised
42+
requirements_valid_py_not_init <- all(requirements_valid) && py_not_init
43+
if (requirements_valid_py_not_init) {
4444

4545
cli_process_done(
4646
msg_done = "Initialising python and checking dependencies ... done!")
4747
cat("\n")
4848
greta_stash$python_has_been_initialised <- TRUE
4949

50-
}
51-
5250
}
5351

5452
if (!all(requirements_valid)) {
@@ -100,7 +98,7 @@ check_dims <- function(...,
10098

10199
# as text, for printing
102100
dims_paste <- vapply(dim_list, paste, "", collapse = "x")
103-
dims_text <- paste(dims_paste, collapse = ", ")
101+
dims_text <- toString(dims_paste)
104102

105103
# which are scalars
106104
scalars <- vapply(elem_list, is_scalar, FALSE)
@@ -734,7 +732,7 @@ check_dependencies_satisfied <- function(target,
734732

735733
# build the message
736734
if (any(matches)) {
737-
names_text <- paste(unmet_names, collapse = ", ")
735+
names_text <- toString(unmet_names)
738736
msg <- cli::format_error(
739737
message = c(
740738
"Please provide values for the following {length(names_text)} \\
@@ -1972,7 +1970,7 @@ check_is_greta_array <- function(x,
19721970
check_missing_infinite_values <- function(x,
19731971
optional,
19741972
call = rlang::caller_env()){
1975-
contains_missing_or_inf <- !optional & any(!is.finite(x))
1973+
contains_missing_or_inf <- !optional & !all(is.finite(x))
19761974
if (contains_missing_or_inf) {
19771975
cli::cli_abort(
19781976
message = c(

R/distribution.R

+9-9
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,19 @@
8888
}
8989

9090
# if distribution isn't scalar, make sure it has the right dimensions
91-
## TODO fix explaining variable
92-
if (!is_scalar(value)) {
93-
if (!identical(dim(greta_array), dim(value))) {
94-
cli::cli_abort(
95-
c(
96-
"left and right hand sides have different dimensions. ",
97-
"The distribution must have dimension of either \\
91+
not_scalar <- !is_scalar(value)
92+
lhs_rhs_different_dim <- !identical(dim(greta_array), dim(value))
93+
not_scalar_different_dims <- not_scalar && lhs_rhs_different_dim
94+
if (not_scalar_different_dims) {
95+
cli::cli_abort(
96+
c(
97+
"left and right hand sides have different dimensions. ",
98+
"The distribution must have dimension of either \\
9899
{.val {paste(dim(greta_array), collapse = 'x')}} or {.val 1x1},\\
99100
but instead has dimension \\
100101
{.val {paste(dim(value), collapse = 'x')}}"
101-
)
102102
)
103-
}
103+
)
104104
}
105105

106106
# assign the new node as the distribution's target

R/extract_replace_combine.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ NULL
100100
dummy_out <- do.call(.Primitive("["), call_list, envir = pf)
101101
rm("._dummy_in", envir = pf)
102102

103-
if (any(is.na(dummy_out))) {
103+
if (anyNA(dummy_out)) {
104104
cli::cli_abort(
105105
"subscript out of bounds"
106106
)
@@ -196,7 +196,7 @@ NULL
196196
dummy_out <- do.call(.Primitive("["), call_list, envir = pf)
197197
rm("._dummy_in", envir = pf)
198198

199-
if (any(is.na(dummy_out))) {
199+
if (anyNA(dummy_out)) {
200200
cli::cli_abort(
201201
"subscript out of bounds"
202202
)
@@ -211,7 +211,7 @@ NULL
211211
"number of items to replace is not a multiple of replacement length"
212212
)
213213
} else {
214-
replacement <- rep(replacement, length.out = length(index))
214+
replacement <- rep_len(replacement, length(index))
215215
}
216216
}
217217

@@ -542,7 +542,7 @@ length.greta_array <- function(x) {
542542
dims <- c(dims, 1L)
543543
}
544544

545-
if (any(is.na(dims))) {
545+
if (anyNA(dims)) {
546546
cli::cli_abort(
547547
"the dims contain missing values"
548548
)

R/inference.R

+6-4
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,12 @@ mcmc <- function(
249249
max_samplers <- future::nbrOfWorkers()
250250

251251
# divide chains up between the workers
252-
chain_assignment <- sort(rep(
253-
seq_len(max_samplers),
254-
length.out = chains
255-
))
252+
chain_assignment <- sort(
253+
rep_len(
254+
seq_len(max_samplers),
255+
length.out = chains
256+
)
257+
)
256258

257259
# divide the initial values between them
258260
initial_values_split <- split(initial_values, chain_assignment)

R/inference_class.R

-3
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,6 @@ hmc_sampler <- R6Class(
914914
return(
915915
sampler_kernel
916916
)
917-
# nolint end
918917
},
919918
sampler_parameter_values = function() {
920919

@@ -999,7 +998,6 @@ rwmh_sampler <- R6Class(
999998
return(
1000999
sampler_kernel
10011000
)
1002-
# nolint end
10031001
},
10041002
sampler_parameter_values = function() {
10051003
epsilon <- self$parameters$epsilon
@@ -1044,7 +1042,6 @@ slice_sampler <- R6Class(
10441042
return(
10451043
sampler_kernel
10461044
)
1047-
# nolint end
10481045
},
10491046
sampler_parameter_values = function() {
10501047
max_doublings <- as.integer(self$parameters$max_doublings)

R/progress_bar.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ create_progress_bar <- function(phase, iter, pb_update, width, ...) {
2424
# pad the frmat so that the width iterations counter is the same for both
2525
# warmup and sampling
2626
digit_diff <- nchar(max(iter)) - nchar(iter_this)
27-
count_pad <- paste0(rep(" ", 2 * digit_diff), collapse = "")
27+
count_pad <- paste(rep(" ", 2 * digit_diff), collapse = "")
2828

2929
# formatting
3030
format_text <- glue::glue(
@@ -73,7 +73,7 @@ iterate_progress_bar <- function(pb, it, rejects, chains, file = NULL) {
7373
}
7474
# pad the end of the line to keep the update bar a consistent width
7575
pad_char <- pmax(0, 2 - nchar(reject_perc_string))
76-
pad <- paste0(rep(" ", pad_char), collapse = "")
76+
pad <- paste(rep(" ", pad_char), collapse = "")
7777

7878
reject_text <- glue::glue("| {reject_perc_string}% bad{pad}")
7979
} else {

R/unknowns_class.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ print.unknowns <- function(x, ..., n = 10) {
5656
}
5757

5858
# create an unknowns array from some dimensions
59-
unknowns <- function(dims = c(1, 1), data = as.numeric(NA)) {
59+
unknowns <- function(dims = c(1, 1), data = NA_real_) {
6060
x <- array(data = data, dim = dims)
6161
as.unknowns(x)
6262
}

R/utils.R

+5-4
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,10 @@ is_using_cpu <- function(x){
991991
`%||%` <- function(x, y) if (is.null(x)) y else x
992992

993993
message_if_using_gpu <- function(compute_options){
994-
if (is_using_gpu(compute_options)) {
995-
if (getOption("greta_gpu_message") %||% TRUE){
994+
gpu_used <- is_using_gpu(compute_options)
995+
greta_gpu_message <- getOption("greta_gpu_message") %||% TRUE
996+
gpu_used_and_message <- gpu_used && greta_gpu_message
997+
if (gpu_used_and_message) {
996998
cli::cli_inform(
997999
c(
9981000
"NOTE: When using GPU, the random number seed may not always be \\
@@ -1003,7 +1005,6 @@ message_if_using_gpu <- function(compute_options){
10031005
"{.code options(greta_gpu_message = FALSE)}"
10041006
)
10051007
)
1006-
}
10071008
}
10081009
}
10091010

@@ -1169,7 +1170,7 @@ pretty_dim <- function(x){
11691170
x_dim <- dim(x)
11701171
print_dim_x <- x_dim %||% x
11711172

1172-
prettied_dim <- paste0(print_dim_x, collapse = "x")
1173+
prettied_dim <- paste(print_dim_x, collapse = "x")
11731174
prettied_dim
11741175
}
11751176

flint/cache_file_state.rds

490 Bytes
Binary file not shown.

flint/config.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
keep:
2+
- any_duplicated
3+
- any_is_na
4+
- class_equals
5+
- condition_message
6+
- double_assignment
7+
- duplicate_argument
8+
- empty_assignment
9+
- equal_assignment
10+
- equals_na
11+
- expect_comparison
12+
- expect_identical
13+
- expect_length
14+
- expect_named
15+
- expect_not
16+
- expect_null
17+
- expect_true_false
18+
- expect_type
19+
- for_loop_index
20+
- function_return
21+
- implicit_assignment
22+
- is_numeric
23+
- length_levels
24+
- length_test
25+
- lengths
26+
- library_call
27+
- list_comparison
28+
- literal_coercion
29+
- matrix_apply
30+
- missing_argument
31+
- nested_ifelse
32+
- numeric_leading_zero
33+
- outer_negation
34+
- package_hooks
35+
- paste
36+
- redundant_equals
37+
- redundant_ifelse
38+
- rep_len
39+
- right_assignment
40+
- sample_int
41+
- semicolon
42+
- seq
43+
- sort
44+
- stopifnot_all
45+
- T_and_F_symbol
46+
- todo_comment
47+
- undesirable_function
48+
- undesirable_operator
49+
- unnecessary_nesting
50+
- unreachable_code
51+
- which_grepl
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
id: true_false_symbol
2+
language: r
3+
severity: warning
4+
rule:
5+
pattern: T
6+
kind: identifier
7+
not:
8+
any:
9+
- precedes:
10+
any:
11+
- pattern: <-
12+
- pattern: =
13+
- regex: ^~$
14+
- follows:
15+
any:
16+
- pattern: $
17+
- regex: ^~$
18+
- inside:
19+
any:
20+
- kind: parameter
21+
- kind: call
22+
- kind: binary_operator
23+
follows:
24+
regex: ^~$
25+
stopBy: end
26+
stopBy:
27+
kind:
28+
argument
29+
fix: TRUE
30+
message: Use TRUE instead of the symbol T.
31+
32+
---
33+
34+
id: true_false_symbol-2
35+
language: r
36+
severity: warning
37+
rule:
38+
pattern: F
39+
kind: identifier
40+
not:
41+
any:
42+
- precedes:
43+
any:
44+
- pattern: <-
45+
- pattern: =
46+
- regex: ^~$
47+
- follows:
48+
any:
49+
- pattern: $
50+
- regex: ^~$
51+
- inside:
52+
any:
53+
- kind: parameter
54+
- kind: call
55+
- kind: binary_operator
56+
follows:
57+
regex: ^~$
58+
stopBy: end
59+
stopBy:
60+
kind:
61+
argument
62+
fix: FALSE
63+
message: Use FALSE instead of the symbol F.
64+
65+
---
66+
67+
id: true_false_symbol-3
68+
language: r
69+
severity: warning
70+
rule:
71+
pattern: T
72+
kind: identifier
73+
precedes:
74+
any:
75+
- pattern: <-
76+
- pattern: =
77+
not:
78+
inside:
79+
kind: argument
80+
message: Don't use T as a variable name, as it can break code relying on T being TRUE.
81+
82+
---
83+
84+
id: true_false_symbol-4
85+
language: r
86+
severity: warning
87+
rule:
88+
pattern: F
89+
kind: identifier
90+
precedes:
91+
any:
92+
- pattern: <-
93+
- pattern: =
94+
not:
95+
inside:
96+
kind: argument
97+
message: Don't use F as a variable name, as it can break code relying on F being FALSE.

flint/rules/builtin/absolute_path.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# id: absolute_path-1
2+
# language: r
3+
# severity: warning
4+
# rule:
5+
# kind: string_content
6+
# any:
7+
# - regex: '^~[[:alpha:]]'
8+
# - regex: '^~/[[:alpha:]]'
9+
# - regex: '^[[:alpha:]]:'
10+
# - regex: '^(/|~)$'
11+
# - regex: '^/[[:alpha:]]'
12+
# - regex: '^\\'
13+
# message: Do not use absolute paths.

0 commit comments

Comments
 (0)