-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcommon.R
89 lines (83 loc) · 1.81 KB
/
common.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
print(here::here())
knitr::opts_chunk$set(
digits = 3,
comment = "#>",
dev = 'svglite',
dev.args = list(bg = "transparent"),
fig.path = "figs/",
fig.align = "center",
collapse = TRUE
)
options(width = 80, cli.width = 70)
article_req_pkgs <- function(x, what = "To use code in this article, ") {
x <- sort(x)
x <- knitr::combine_words(x, and = " and ")
paste0(
what,
" you will need to install the following packages: ",
x,
"."
)
}
small_session <- function(pkgs = NULL) {
pkgs <- c(
pkgs,
"recipes",
"parsnip",
"tune",
"workflows",
"dials",
"dplyr",
"broom",
"ggplot2",
"purrr",
"rlang",
"rsample",
"tibble",
"infer",
"yardstick",
"tidymodels",
"infer"
)
pkgs <- unique(pkgs)
library(sessioninfo)
library(dplyr)
sinfo <- sessioninfo::session_info()
cls <- class(sinfo$packages)
sinfo$packages <-
sinfo$packages %>%
dplyr::filter(package %in% pkgs)
class(sinfo$packages) <- cls
remove_double_newlines <- function(x) {
ind <- x == ""
count <- 0
for (i in seq_along(ind)) {
if (ind[i]) {
count <- count + 1
if (count == 1) {
ind[i] <- FALSE
}
} else {
count <- 0
}
}
x[!ind]
}
sinfo <- capture.output(sinfo)
sinfo <- sinfo |>
stringr::str_subset("^ \\[\\d+\\] ", negate = TRUE) |>
stringr::str_subset(
"^ (setting|os|system|ui|collate|ctype|tz)",
negate = TRUE
) |>
stringr::str_remove(" @ .*") |>
stringr::str_replace_all("\\*", " ") |>
stringr::str_replace("lib source", "source") |>
stringr::str_replace(" \\[\\d+\\] ", " ") |>
stringr::str_subset(
"Packages attached to the search path",
negate = TRUE
) |>
remove_double_newlines()
cat(sinfo, sep = "\n")
}