Customizing Table Formatting with rtables and tern in R: A Step-by-Step Guide

The provided code appears to be a solution for creating a table with custom formatting using the rtables and tern packages in R.

Here’s an explanation of the code:

  1. The first section imports the necessary packages: formatters, rtables, and tern.
  2. A new data frame advs is created by subseting a larger data frame (ex_advs) to include only rows where the COUNTRY column is either “CHN” or “USA”.
  3. The custom_format function is defined, which takes in an object x and returns a formatted string. This function checks if the “num” column of x is 0 and returns a string like “0/1”. If not, it calculates the proportion of rows with non-zero values and returns a string like “2/3”.
  4. The custom_afun_count_occurrences function is defined, which takes in several arguments, including df, denom, .N_col, .N_row, .df_row, drop, .var, and id. This function analyzes the table using the analyze function from rtables, applying the custom formatting function (custom_format) to each column. The resulting data frame is then returned.
  5. In the last section, the basic_table() function is used to create a basic table, which is then split by columns using the split_cols_by function from rtables. The add_colcounts and analyze functions are applied to this table, passing in the custom formatting function (custom_afun_count_occurrences) as an argument.
  6. Finally, the resulting data frame is passed to the build_table function from rtables, which creates a new table with the custom formatting applied.

The final output of this code is a table with customized formatting for each column, where the proportion of rows with non-zero values is displayed as a string in the format “num/denom”.

Example use case:

Suppose we have a data frame df containing country-specific data, and we want to create a table that displays the number of patients in each country. We can modify the code above to suit our needs by changing the column names and formatting as needed.

library(rtables)
library(tern)

# Create a sample data frame
df <- data.frame(country = c("CHN", "USA", "CHN", "USA"),
                 patient = c(100, 200, 50, 150))

# Apply custom formatting function to each column
custom_format <- function(x) {
  if (x == 0) {
    return(paste(x, "/", x))
  } else {
    num <- sum(x)
    den <- nrow(df)
    prop <- round(num / den * 100, 1)
    paste(num, "/", den, "(", prop, "%)")
  }
}

custom_afun_count_occurrences <- function(df, denom = c("N_col", "n", "N_row"), 
                                          .N_col, .N_row, .df_row, drop = TRUE, 
                                          .var = "country", id = "patient") {
  # ... (same code as before)
}

# Apply custom formatting to each column
tbl <- rtables::build_table(df %>% 
                             rtables::split_cols_by(var = country) %>% 
                             rtables::add_colcounts() %>% 
                             rtables::analyze(afun = custom_afun_count_occurrences, format = list(fraction = custom_format)), id = patient)

# Print the resulting table
print(tbl)

Last modified on 2024-04-18