This function takes a data frame as input and converts it to a format catalog based on the information contained in the data frame. The data frame should have 5 columns: "Name", "Type", "Expression", "Label" and "Order".

# S3 method for tbl_df
as.fcat(x)

Arguments

x

The data frame to convert.

Value

A format catalog based on the information contained in the input data frame.

Details

The as.fcat.data.frame converts a data frame to a format catalog. A corresponding conversion for class "tbl_df" converts a tibble.

To understand the structure of the input data frame, create a format and use the as.data.frame method to convert the format to a data frame. Then observe the columns and organization of the data.

Input Data Frame Specifications

The input data frame should contain the following columns:

  • Name: The name of the format

  • Type: The type of format. See the type codes below.

  • Expression: The formatting expression. The expression will hold different types of values depending on the format type.

  • Label: The label for user-defined, "U" type formats.

  • Order: The order for user-defined, "U" type formats.

Any additional columns will be ignored. Column names are case-insensitive.

Valid values for the "Type" column are as follows:

  • U: User Defined List created with the value function.

  • S: A formatting string of formatting codes. See FormattingStrings.

  • F: A vectorized function.

  • V: A named vector lookup.

The "Label" and "Order" columns are used only for a type "U", user-defined format created with the value function.

Examples

# Create a format catalog
c1 <- fcat(num_fmt  = "%.1f",
           label_fmt = value(condition(x == "A", "Label A"),
                             condition(x == "B", "Label B"),
                             condition(TRUE, "Other")),
           date_fmt = "%d-%b-%Y")
           
# Convert catalog to data frame to view the structure
df <- as.data.frame(c1)
print(df)
#       Name Type Expression   Label Order
# 1   num_fmt    S       %.1f            NA
# 2 label_fmt    U   x == "A" Label A    NA
# 3 label_fmt    U   x == "B" Label B    NA
# 4 label_fmt    U       TRUE   Other    NA
# 5  date_fmt    S   %d-%b-%Y            NA

# Convert data frame back to a format catalog
c2 <- as.fcat(df)
c2
# # A format catalog: 3 formats
# - $date_fmt: type S, "%d-%b-%Y"
# - $label_fmt: type U, 3 conditions
# - $num_fmt: type S, "%.1f"

# Use re-converted catalog
fapply(123.456, c2$num_fmt)
# [1] "123.5"

fapply(c("A", "B", "C", "B"), c2$label_fmt)
# [1] "Label A" "Label B" "Other"   "Label B"

fapply(Sys.Date(), c2$date_fmt)
# [1] "07-Jan-2024"