Skip to content

Commit

Permalink
create metalite_table1_to_rtf function
Browse files Browse the repository at this point in the history
  • Loading branch information
elong0527 committed Dec 2, 2023
1 parent 3cca757 commit d5eb847
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
47 changes: 47 additions & 0 deletions R/metalite_table1_to_rtf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' Convert `metalite_table1` to an RTF file
#'
#' @param x Output of `metalite_table1`.
#' @inheritParam r2rtf::write_rtf
#' @inheritParam r2rtf::rtf_title
#' @inheritParam r2rtf::rtf_body
#'
#' @return a string of the RTF file path
#'
#' @export
metalite_table1_to_rtf <- function(
x,
file,
title = "Baseline Characteristics",
rel_width = NULL){

# prepare input
tbl <- lapply(tbl[[1]], reactable_to_df)

colheader <- attr(tbl[[1]], "column_header")
tbl <- do.call(rbind, tbl)
rownames(tbl) <- NULL

n_col <- ncol(tbl)

if(is.null(rel_width)){
rel_width = c(5, rep(2, n_col - 1))
}

# output RTF
tbl |>
r2rtf::rtf_title(title) |>
r2rtf::rtf_colheader(colheader,
col_rel_width = rel_width
) |>
r2rtf::rtf_body(
col_rel_width = rel_width,
text_justification = c("l", rep("c", n_col - 1)),
text_indent_first = -240,
text_indent_left = 180
) |>
r2rtf::rtf_encode() |>
r2rtf::write_rtf(file)

return(file)
}

1 change: 1 addition & 0 deletions R/reactable2.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ reactable_to_df <- function(x){
# output
tbl <- data.frame(tbl1[, tbl2[["id"]]])

tbl[, 1] <- gsub("\U2000", " ", tbl[, 1])
attr(tbl, "column_header") <- paste(tbl2$name, collapse = "|")

tbl
Expand Down

0 comments on commit d5eb847

Please sign in to comment.