Skip to content

Commit 1dcb7e6

Browse files
committed
Update documentation.
1 parent 77b8a3e commit 1dcb7e6

134 files changed

Lines changed: 6869 additions & 1761 deletions

File tree

Some content is hidden

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

DESCRIPTION

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: fmtr
22
Type: Package
33
Title: Easily Apply Formats to Data
4-
Version: 1.6.2
4+
Version: 1.6.3
55
Author: David Bosak
66
Maintainer: David Bosak <dbosak01@gmail.com>
77
Description: Contains a set of functions that can be used to apply
@@ -24,7 +24,9 @@ Suggests:
2424
rmarkdown,
2525
covr,
2626
logr,
27-
utils
27+
utils,
28+
dplyr,
29+
libr
2830
Imports:
2931
tibble,
3032
stats,

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# fmtr 1.6.3
2+
3+
* Improved documentation and examples.
4+
15
# fmtr 1.6.2
26

37
* Added "as.factor" parameter to `value()` function to convert results to a factor.

R/descriptions.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,34 @@
3535
#' @aliases descriptions<-
3636
#' @examples
3737
#' # Take subset of data
38-
#' df1 <- mtcars[1:10, c("mpg", "cyl") ]
38+
#' df1 <- mtcars[1:5, c("mpg", "cyl") ]
3939
#'
4040
#' # Print current state
4141
#' print(df1)
42+
#' # mpg cyl
43+
#' # Mazda RX4 21.0 6
44+
#' # Mazda RX4 Wag 21.0 6
45+
#' # Datsun 710 22.8 4
46+
#' # Hornet 4 Drive 21.4 6
47+
#' # Hornet Sportabout 18.7 8
4248
#'
4349
#' # Assign descriptions
4450
#' descriptions(df1) <- list(mpg = "Miles per Gallon", cyl = "Cylinders")
4551
#'
4652
#' # Display descriptions
4753
#' descriptions(df1)
54+
#' # $mpg
55+
#' # [1] "Miles per Gallon"
56+
#' #
57+
#' # $cyl
58+
#' # [1] "Cylinders"
4859
#'
4960
#' # Clear descriptions
5061
#' descriptions(df1) <- NULL
5162
#'
5263
#' # Confirm descriptions are cleared
5364
#' descriptions(df1)
65+
#' # list()
5466
descriptions <- function(x) {
5567

5668
ret <- list()

R/fapply.R

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,44 +65,44 @@
6565
#'
6666
#' # Apply string format.
6767
#' fapply(v1, "%.1f")
68-
#'
68+
#' # [1] "1.2" "8.4" "6.0" "2.5"
6969
#'
7070
#' ## Example 2: Named vector ##
7171
#' # Set up vector
7272
#' v2 <- c("A", "B", "C", "B")
7373
#'
7474
#' # Set up named vector for formatting
75-
#' fmt2 <- c(A = "Vector Label A", B = "Vector Label B", C = "Vector Label C")
75+
#' fmt2 <- c(A = "Label A", B = "Label B", C = "Label C")
7676
#'
7777
#' # Apply format to vector
7878
#' fapply(v2, fmt2)
79-
#'
79+
#' # [1] "Label A" "Label B" "Label C" "Label B"
8080
#'
8181
#' ## Example 3: User-defined format ##
8282
#' # Define format
83-
#' fmt3 <- value(condition(x == "A", "Format Label A"),
84-
#' condition(x == "B", "Format Label B"),
85-
#' condition(TRUE, "Format Other"))
83+
#' fmt3 <- value(condition(x == "A", "Label A"),
84+
#' condition(x == "B", "Label B"),
85+
#' condition(TRUE, "Other"))
8686
#'
8787
#' # Apply format to vector
8888
#' fapply(v2, fmt3)
89-
#'
89+
#' # [1] "Label A" "Label B" "Other" "Label B"
9090
#'
9191
#' ## Example 4: Formatting function ##
9292
#' # Set up vectorized function
9393
#' fmt4 <- Vectorize(function(x) {
9494
#'
9595
#' if (x %in% c("A", "B"))
96-
#' ret <- paste("Function Label", x)
96+
#' ret <- paste("Label", x)
9797
#' else
98-
#' ret <- "Function Other"
98+
#' ret <- "Other"
9999
#'
100100
#' return(ret)
101101
#' })
102102
#'
103103
#' # Apply format to vector
104104
#' fapply(v2, fmt4)
105-
#'
105+
#' # [1] "Label A" "Label B" "Other" "Label B"
106106
#'
107107
#' ## Example 5: Formatting List - Row Type ##
108108
#' # Set up data
@@ -122,18 +122,34 @@
122122
#'
123123
#' # Apply formatting list to vector
124124
#' fapply(v3, lst)
125-
#'
125+
#' # [1] "2,841.3" "High" "06Jan2024" "Low" "06Mar2024" "1,382.9"
126126
#'
127127
#' ## Example 6: Formatting List - Column Type ##
128128
#' # Set up data
129129
#' v5 <- c(Sys.Date(), Sys.Date() + 30, Sys.Date() + 60)
130130
#' v5
131+
#' # [1] "2024-01-06" "2024-02-05" "2024-03-06"
131132
#'
132133
#' # Create formatting list
133134
#' lst <- flist("%B", "This month is: %s", type = "column")
134135
#'
135136
#' # Apply formatting list to vector
136137
#' fapply(v5, lst)
138+
#' # [1] "This month is: January" "This month is: February" "This month is: March"
139+
#'
140+
#' # Example 7: Conditional Formatting
141+
#' # Data vector
142+
#' v6 <- c(8.38371, 1.46938, 3.28783, NA, 0.98632)
143+
#'
144+
#' # User-defined format
145+
#' fmt5 <- value(condition(is.na(x), "Missing"),
146+
#' condition(x < 1, "Low"),
147+
#' condition(x > 5, "High"),
148+
#' condition(TRUE, "%.2f"))
149+
#'
150+
#' # Apply format to data vector
151+
#' fapply(v6, fmt5)
152+
#' # [1] "High" "1.47" "3.29" "Missing" "Low"
137153
fapply <- function(x, format = NULL, width = NULL, justify = NULL) {
138154

139155
# Get attribute values if available

R/fattr.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#'
4040
#' # Apply format attributes
4141
#' fapply(a)
42-
#'
4342
#' # [1] " 1.3 " " 6.0 " " 2.4 "
4443
fattr <- function(x, format = NULL, width = NULL, justify = NULL,
4544
label = NULL, description = NULL, keep = TRUE) {
@@ -103,6 +102,7 @@ fattr <- function(x, format = NULL, width = NULL, justify = NULL,
103102
#'
104103
#' # Apply format attributes
105104
#' fapply(a)
105+
#' # [1] "1.3" "6.0" "2.4"
106106
"fattr<-" <- function(x, value) {
107107

108108
if (!is.null(value[["format"]])) {

R/fcat.R

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@
4444
#'
4545
#' # Use formats in the catalog
4646
#' fapply(2, c1$num_fmt)
47+
#' # [1] "2.0"
48+
#'
4749
#' fapply(c("A", "B", "C", "B"), c1$label_fmt)
50+
#' # [1] "Label A" "Label B" "Other" "Label B"
51+
#'
4852
#' fapply(Sys.Date(), c1$date_fmt)
53+
#' # [1] "06Jan2024"
54+
#'
4955
#' @export
5056
fcat <- function(..., log = TRUE) {
5157

@@ -70,7 +76,7 @@ fcat <- function(..., log = TRUE) {
7076
#' @description A generic method for casting objects to
7177
#' a format catalog. Individual objects will inherit from this function.
7278
#' @param x The object to cast.
73-
#' @return A formatting object, created using the information in the
79+
#' @return A format catalog, created using the information in the
7480
#' input object.
7581
#' @seealso For class-specific methods, see \code{\link{as.fcat.data.frame}},
7682
#' \code{\link{as.fcat.list}}, and \code{\link{as.fcat.fmt_lst}}.
@@ -130,7 +136,6 @@ as.fcat <- function (x) {
130136
#' # Convert catalog to data frame to view the structure
131137
#' df <- as.data.frame(c1)
132138
#' print(df)
133-
#'
134139
#' # Name Type Expression Label Order
135140
#' # 1 num_fmt S %.1f NA
136141
#' # 2 label_fmt U x == "A" Label A NA
@@ -140,11 +145,21 @@ as.fcat <- function (x) {
140145
#'
141146
#' # Convert data frame back to a format catalog
142147
#' c2 <- as.fcat(df)
148+
#' c2
149+
#' # # A format catalog: 3 formats
150+
#' # - $date_fmt: type S, "%d-%b-%Y"
151+
#' # - $label_fmt: type U, 3 conditions
152+
#' # - $num_fmt: type S, "%.1f"
143153
#'
144154
#' # Use re-converted catalog
145155
#' fapply(123.456, c2$num_fmt)
156+
#' # [1] "123.5"
157+
#'
146158
#' fapply(c("A", "B", "C", "B"), c2$label_fmt)
159+
#' # [1] "Label A" "Label B" "Other" "Label B"
160+
#'
147161
#' fapply(Sys.Date(), c2$date_fmt)
162+
#' # [1] "07-Jan-2024"
148163
#' @export
149164
as.fcat.data.frame <- function(x) {
150165

@@ -250,7 +265,6 @@ as.fcat.fmt_lst <- function(x) {
250265
#' # Convert catalog to data frame to view the structure
251266
#' df <- as.data.frame(c1)
252267
#' print(df)
253-
#'
254268
#' # Name Type Expression Label Order
255269
#' # 1 num_fmt S %.1f NA
256270
#' # 2 label_fmt U x == "A" Label A NA
@@ -260,6 +274,11 @@ as.fcat.fmt_lst <- function(x) {
260274
#'
261275
#' # Convert data frame back to a format catalog
262276
#' c2 <- as.fcat(df)
277+
#' c2
278+
#' # # A format catalog: 3 formats
279+
#' # - $date_fmt: type S, "%d%b%Y"
280+
#' # - $label_fmt: type U, 3 conditions
281+
#' # - $num_fmt: type S, "%.1f"
263282
#' @export
264283
as.data.frame.fcat <- function(x, row.names = NULL, optional = FALSE, ...) {
265284

@@ -353,8 +372,13 @@ as.data.frame.fcat <- function(x, row.names = NULL, optional = FALSE, ...) {
353372
#'
354373
#' # Use formats in the catalog
355374
#' fapply(2, c1$num_fmt)
375+
#' # [1] "2.0"
376+
#'
356377
#' fapply(c("A", "B", "C", "B"), c1$label_fmt)
378+
#' # [1] "Label A" "Label B" "Other" "Label B"
379+
#'
357380
#' fapply(Sys.Date(), c1$date_fmt)
381+
#' # [1] "07Jan2024"
358382
#' @export
359383
write.fcat <- function(x, dir_path = getwd(), file_name = NULL) {
360384

@@ -406,8 +430,13 @@ write.fcat <- function(x, dir_path = getwd(), file_name = NULL) {
406430
#'
407431
#' # Use formats in the catalog
408432
#' fapply(2, c1$num_fmt)
433+
#' # [1] "2.0"
434+
#'
409435
#' fapply(c("A", "B", "C", "B"), c1$label_fmt)
436+
#' # [1] "Label A" "Label B" "Other" "Label B"
437+
#'
410438
#' fapply(Sys.Date(), c1$date_fmt)
439+
#' # [1] "07Jan2024"
411440
#' @export
412441
read.fcat <- function(file_path) {
413442

@@ -442,6 +471,10 @@ read.fcat <- function(file_path) {
442471
#'
443472
#' # Print the catalog
444473
#' print(c1)
474+
#' # # A format catalog: 3 formats
475+
#' # - $num_fmt: type S, "%.1f"
476+
#' # - $label_fmt: type U, 3 conditions
477+
#' # - $date_fmt: type S, "%d%b%Y"
445478
#' @import crayon
446479
#' @export
447480
print.fcat <- function(x, ..., verbose = FALSE) {
@@ -524,7 +557,10 @@ print.fcat <- function(x, ..., verbose = FALSE) {
524557
#'
525558
#' # Test for "fcat" class
526559
#' is.fcat(c1)
527-
#' is.fcat(Sys.Date())
560+
#' # [1] TRUE
561+
#'
562+
#' is.fcat(Sys.Date())
563+
#' # [1] FALSE
528564
#' @export
529565
is.fcat <- function(x) {
530566

R/fdata.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
#'
5656
#' # Before formatting
5757
#' df
58-
#'
5958
#' # state area pct
6059
#' # 1 AL 51609 1.42629378
6160
#' # 2 AK 589757 16.29883824
@@ -79,7 +78,6 @@
7978
#'
8079
#' # Apply formats
8180
#' fdata(df)
82-
#'
8381
#' # state area pct
8482
#' # 1 Alabama 51,609 1.4%
8583
#' # 2 Alaska 589,757 16.3%

R/flist.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#'
5252
#' # Apply formatting list to vector
5353
#' fapply(v1, fl1)
54-
#'
5554
#' # [1] "The month is: October" "The month is: November" "The month is: December"
5655
#'
5756
#' ## Example 2: Formatting List - Row Type ordered ##
@@ -67,7 +66,6 @@
6766
#' "%d%b%Y")
6867
#'
6968
#' fapply(l1, fl2)
70-
#'
7169
#' # [1] "Label A" "1.3" "21Jul2020" "Label B" "5.9" "17Oct2020"
7270
#'
7371
#'
@@ -89,7 +87,6 @@
8987
#'
9088
#' # Apply formatting list to vector, using lookup
9189
#' fapply(l2, fl3)
92-
#'
9390
#' # [1] "2,841.3" "High" "19Jun2020" "Low" "24Apr2020" "1,382.9"
9491
flist <- function(..., type = "column", lookup = NULL, simplify = TRUE) {
9592

@@ -295,7 +292,6 @@ as.flist.fcat <- function(x, type = "column", lookup = NULL, simplify = TRUE) {
295292
#' # Convert catalog to data frame to view the structure
296293
#' df <- as.data.frame(c1)
297294
#' print(df)
298-
#'
299295
#' # Name Type Expression Label Order
300296
#' # 1 num_fmt S %.1f NA
301297
#' # 2 label_fmt U x == "A" Label A NA
@@ -305,6 +301,16 @@ as.flist.fcat <- function(x, type = "column", lookup = NULL, simplify = TRUE) {
305301
#'
306302
#' # Convert data frame back to a formatting list
307303
#' c2 <- as.flist(df)
304+
#' c2
305+
#' # # A formatting list: 3 formats
306+
#' # - type: column
307+
#' # - simplify: TRUE
308+
#' # Name Type Expression Label Order
309+
#' # 1 date_fmt S %d%b%Y <NA>
310+
#' # 2 label_fmt U x == "A" Label A <NA>
311+
#' # 3 label_fmt U x == "B" Label B <NA>
312+
#' # 4 label_fmt U TRUE Other <NA>
313+
#' # 5 num_fmt S %.1f <NA>
308314
#' @export
309315
as.data.frame.fmt_lst <- function(x, row.names = NULL, optional = FALSE, ...) {
310316

0 commit comments

Comments
 (0)