最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - How to keep compact gtsummary theme while having superscripts in labels? - Stack Overflow

programmeradmin1浏览0评论

I have created a gtsummary table using the theme_gtsummary_compact() and would like to show a superscript in one of my variable labels. This last part works with the fmt_markdown() function but I then loose the compact table design.

Here is example code:

library(dplyr)
library(gtsummary)
library(gt)

# Set seed for reproducibility
set.seed(123)

# Create data set
mock_data <- tibble(
  age = sample(18:90, 100, replace = TRUE),
  ethnicity = sample (c("White","Black","Asian","Hispanic","Other"), 100, replace = TRUE),
  bmi = round(runif(100, 18, 40), 1),
  severity = sample(c("Mild","Moderate","Severe"), 100, replace = TRUE)

)

# Create gtsummary table
theme_gtsummary_compact()
summary_table <- mock_data %>% 
  tbl_summary(
    by = severity,
    label = list(
      age ~ "Age (in years)",
      ethnicity ~ "Ethnicity",
      bmi ~ "BMI (kg/m<sup>2</sup>)",
      severity ~ "Disease Severity"
    )
  ) %>% 
  add_p()
summary_table

# Convert to gt table and format HTML labels
gt_table <- summary_table %>% 
  as_gt() %>% 
  fmt_markdown(columns = c(label))
gt_table

When I add (after the fmt_markdown) tab_options for fonts, row.padding etc, I get a partially compacted table (for the numeric variables, but not for the individual categories of any categorical variable). I also loose other attributes of the theme, for example the indentations for the individual character levels. What is the best solution to get the table to resemble the original compact table but with the superscript in the label?

gt_table <- summary_table %>% 
  as_gt() %>% 
  fmt_markdown(columns = c(label)) %>% 
  tab_options(
    table.font.size = "small",
    heading.title.font.size = "small",
    heading.subtitle.font.size = "small",
    table.border.top.width = px(0),
    table.border.bottom.width = px(0),
    column_labels.border.top.width = px(0),
    column_labels.border.bottom.width = px(0),
    data_row.padding = px(1),
    summary_row.padding = px(1)
  ) 
gt_table

And lastly, how would we export this table successfully to a Word document while retaining any and all formats?

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>