Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facilitate the format setting for complicated tables/listings #238

Open
Rednose22 opened this issue Jan 7, 2025 · 4 comments
Open

Facilitate the format setting for complicated tables/listings #238

Rednose22 opened this issue Jan 7, 2025 · 4 comments
Labels
enhancement New feature or request

Comments

@Rednose22
Copy link

Is your feature request related to a problem? Please describe.
It’s a bit lengthy and tricky to add the format attributes into the dataframe following the paradigm of r2rtf, especially for the complicated table outputs with multiple levels of header or different format for specific rows or columns.

Describe the solution you'd like
We could have a folder/object to contain the format setting for each kinds of tables. When we generate the output, we could use the do.call() to assign the attributes into the dataframe based on the format settings in the gallery. Below is an example to assign the attributes of header into the dataframe.

## List of raw header information
colheader_list <- list(
   list(
     header =  c("", "col1",
     "col2",
     "col3",
     "col4"),
     match_col_width = c(1, 2, 2, 3, 3, 4, 4, 5, 5),
     style = 'b'),
   list(
     header =  c("", rep(c("n", "(%)"), 4)),
     match_col_width = c(1:9),
     no_border_left = c(3, 5, 7, 9),
     no_border_top = c(1),
     style = 'b')
 )

## Convert the meta information of header to be fit in the r2rtf
header_format <- utils_rtf_header_format(
   colheader_list = colheader_list,
   column_width = column_width[-length(column_width)]
 )

## Function to assign all headers' attributes into the dataframe
utils_rtf_header_combiner <- function(tbl,
                                header_format = header_format) {
  
  pipe_header <- 1:length(header_format)
  names(pipe_header) <- paste0('h', pipe_header)
  
  
  ## Define the function for each pipe. ----
  header_exec <-
    function(header,
             text_format,
             text_justification,
             col_rel_width,
             border_left,
             border_top,
             text_font_size) {

      tbl |> r2rtf::rtf_colheader(
        colheader = header,
        text_format = text_format,
        col_rel_width = col_rel_width,
        text_justification = text_justification,
        border_left = border_left,
        border_top = border_top,
        text_font_size = text_font_size
      )
    }
  
  ## Assign header execution function to every element of header pipeline.
  for (i in 1:length(header_format)) {
    assign(paste0('h', i), header_exec, envir = environment())
  }
  
  ## With do.call function to assign the header format to inputdata.
  for (func in names(pipe_header[order(pipe_header)]))
  {
    tbl <- do.call(func, header_format[[func]])
  }
  
  tbl <- structure(tbl, class = c(class(tbl))) ## preserve the object attributes
  
}

## Load the data
load(file.path(tmppath, 'test.Rdata'))
 
## Assign the attributes dynamically
tbl <- utils_rtf_header_combiner(tbl = test,
                            header_format = header_format)

Describe alternatives you've considered
Further down the line, we could have a formatting gallery for all outputs. So when we generate a specific output, we can directly extract the format setting from gallery and assign it to the outputs.

So I'd like to know if the developers and maintainers are interested in this topic, then we can contribute to this package to add a vignette or some helper functions into the r2rtf package. Thanks.

@Rednose22 Rednose22 added the enhancement New feature or request label Jan 7, 2025
@elong0527
Copy link
Collaborator

If my understand is correct, you have many tables/listings that needs to follow the same and highly customized format.

I feel that's beyond the scope of the r2rtf to just get one table in production ready format.

The framework and examples in metalite.ae and metalite R packages are more relevant.

I'd suggest to extend the metalite or create organization specific helper packages to fit your purposes.

@elong0527
Copy link
Collaborator

Alternatively, if your goal is to create config files to work with r2rtf, using YAML as the source of input may be a better solution.

In that case, an r2rtf vignettes is a good idea to show people some good practice to use the package in production.

@nanxstats
Copy link
Collaborator

I agree, starting from a best practice vignette to offer recipes on automating large-scale production of tables sounds like a good plan without making it too org-specific or a huge commitment. If the recipes happen to come with some reusable functions that would save a lot of typing and boilerplate code, we can incorporate them into the package.

As an example, gt has some API to work with a collection of gt tables although we don't have to model after it.

@Rednose22
Copy link
Author

Thanks for your suggestions @nanxstats, @elong0527, I agree with Nan that a recipes vignette would be good starting point. I will just fork the repo and then make the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants