-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
104ae31
commit cccd2f4
Showing
15 changed files
with
146 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,52 @@ | ||
#' @title gt output for table1 | ||
#' @author Tyson S. Barrett | ||
#' | ||
#' | ||
#' @description This takes a table1 object and outputs a `gt` version. | ||
#' | ||
#' | ||
#' @param tab the table1 object | ||
#' @param spanner the label above the grouping variable (if table1 is grouped) or any label you want to include over the statistics column(s) | ||
#' | ||
#' @param spanner the label above the grouping variable (if table1 is grouped) | ||
#' or any label you want to include over the statistics column(s) | ||
#' | ||
#' @importFrom gt gt | ||
#' @importFrom gt fmt_markdown | ||
#' @importFrom gt tab_spanner | ||
#' | ||
#' | ||
#' @examples | ||
#' | ||
#' | ||
#' library(furniture) | ||
#' library(dplyr) | ||
#' | ||
#' | ||
#' data('nhanes_2010') | ||
#' nhanes_2010 %>% | ||
#' group_by(asthma) %>% | ||
#' table1(age, marijuana, illicit, rehab, na.rm = FALSE) %>% | ||
#' nhanes_2010 %>% | ||
#' group_by(asthma) %>% | ||
#' table1(age, marijuana, illicit, rehab, na.rm = FALSE) %>% | ||
#' table1_gt(spanner = "Asthma") | ||
#' | ||
#' | ||
#' | ||
#' @export | ||
table1_gt <- function(tab, spanner = NULL){ | ||
table1_gt <- function(tab, spanner = NULL) { | ||
# save names and adjust table to include n's in header | ||
nams <- names(tab[[1]]) | ||
nams[1] <- "Characteristic" | ||
tab_df <- as.data.frame(tab) | ||
nams <- paste0(nams, tab_df[1,]) | ||
nams <- paste0(nams, tab_df[1, ]) | ||
nams <- gsub("n =", ", n =", nams) | ||
tab_df <- tab_df[-1, ] | ||
names(tab_df) <- nams | ||
|
||
# add spacing for the table | ||
tab_df$Characteristic <- ifelse(grepl(" ", tab_df$Characteristic), paste(" ", tab_df$Characteristic), tab_df$Characteristic) | ||
|
||
tab_df$Characteristic <- ifelse( | ||
grepl(" ", tab_df$Characteristic), | ||
paste(" ", tab_df$Characteristic), | ||
tab_df$Characteristic | ||
) | ||
|
||
# make it a gt and return | ||
gt_tab <- gt::gt(tab_df) | ||
gt_tab <- gt::fmt_markdown(gt_tab) | ||
|
||
# add spanner | ||
if (!is.null(spanner)) | ||
gt::tab_spanner(gt_tab, label = spanner, columns = -Characteristic) | ||
else | ||
gt_tab | ||
} | ||
|
Oops, something went wrong.