Use Rstudio->File->new Rmarkdown->select word

R Markdown

table creating structure

Summary table with stars for pvalue

Example 1

library(gtsummary)
library(gt)
library(officer)
library(flextable)
## 
## Attaching package: 'flextable'
## The following objects are masked from 'package:gtsummary':
## 
##     as_flextable, continuous_summary
library(magrittr)

tbA <- lm(time ~ ph.ecog + sex, survival::lung) %>% tbl_regression(label = list(ph.ecog = "ECOG Score", sex = "Sex"))

tbA <- lm(time ~ ph.ecog + sex, survival::lung) %>% tbl_regression(label = list(ph.ecog = "ECOG Score", sex = "Sex"))

#add stars for pvalue——
# Example 1 ———————————-

mytbA <- tbA %>% add_significance_stars(hide_ci = FALSE, hide_p = FALSE)

typeof(mytbA)
## [1] "list"
# save to docx
#mytbA

mytbA %>% as_flex_table() %>% flextable::save_as_docx(path="mydoc2.docx")
mytbA
aa=list(1,2,3)
aa2=as.data.frame(aa)
aa3=gt(aa2)
#mytbA=gt(mytbA)
save_as_docx('aa'=aa3,path="mydoc1.docx")

typeof(mytbA)
## [1] "list"
# tf <- tempfile(fileext = ".docx")
# ft1 <- flextable( head( iris ) )
# save_as_docx(ft1, path = tf)
# 
# 
# ft2 <- flextable( head( mtcars ) )
# sect_properties <- prop_section(
#   page_size = page_size(orient = "landscape",
#     width = 8.3, height = 11.7),
#   type = "continuous",
#   page_margins = page_mar()
# )
# save_as_docx(`iris table` = ft1, `mtcars table` = ft2,
#   path = tf, pr_section = sect_properties)

Example 2

mytbB <- tbA %>% add_significance_stars( pattern = "{estimate}
({conf.low}, {conf.high}){stars}", hide_ci = TRUE, hide_se =TRUE ) %>%
modify_header(estimate ~ "**Beta (95% CI)**") %>%
modify_footnote(estimate ~ "CI = Confidence Interval", abbreviation =
TRUE)

mytbB %>% as_flex_table() %>% flextable::save_as_docx(path="mydocfor5.docx")
mytbB

Example 3

tbA=lm(time ~ ph.ecog + sex, survival::lung) %>%
  tbl_regression(label = list(ph.ecog = "ECOG Score", sex = "Sex"))

# Example 3 ----------------------------------
# Use <br> to put a line break between beta and SE in HTML output
mytbC <-
  tbA %>%
  add_significance_stars(
    hide_se = TRUE,
    pattern = "{estimate}{stars}<br>({std.error})"
  ) %>%
  modify_header(estimate ~ "**Beta (SE)**") %>%
  modify_footnote(estimate ~ "SE = Standard Error", abbreviation = TRUE) %>%
  as_gt() %>%
  gt::tab_style(
    style = "vertical-align:top",
    locations = gt::cells_body(columns = label)
  )
#mytbC %>% gt::gtsave(filename = "table15.rtf") 
#mytbC %>% as_flex_table() %>% flextable::save_as_docx(path="mydoc5.docx")
#mytbC
#gt_preview(mytbC)
##as_rtf(mytbC)
gtsave(mytbC,"tab_9.rtf") # doc docx not work but rtf work

#knitr::raw_output(readLines('tab_7.rtf'))
#mytbC2=as.data.frame(mytbC) 
#mytbC3=as_tibble(mytbC2)
#tbl_summary(mytbC3)

# kint to docx in the same time auto open a doc and then  save it 

#tab_1 %>% gtsave("tab_1.docx")

@browning_planning_2019

tab_1 <- gtcars %>% dplyr::select(mfr, model) %>% dplyr::slice(1:2) %>% gt() %>% tab_header( title = md("Data listing from gtcars"), subtitle = md("gtcars is an R dataset") )

tab_1 %>% gtsave("tab_1.docx")

#Other Export Functions: as_latex(), as_raw_html(), extract_summary(), gtsave()

#as_rtf: Output a gt object as RTF

sp500 %>% dplyr::filter( date >= "2015-12-01" & date <= "2015-12-15" ) %>% dplyr::select(-c(adj_close, volume)) %>% gt() %>% tab_style( style = cell_fill(color = "lightgreen"), locations = cells_body(rows = close > open) ) %>%
  tab_style( style = list( cell_fill(color = "red"), cell_text(color = "white") ), locations = cells_body(rows = open > close) )
sp500
## # A tibble: 16,607 × 7
##    date        open  high   low close     volume adj_close
##    <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>     <dbl>
##  1 2015-12-31 2061. 2063. 2044. 2044. 2655330000     2044.
##  2 2015-12-30 2077. 2077. 2062. 2063. 2367430000     2063.
##  3 2015-12-29 2061. 2082. 2061. 2078. 2542000000     2078.
##  4 2015-12-28 2058. 2058. 2044. 2056. 2492510000     2056.
##  5 2015-12-24 2064. 2067. 2059. 2061. 1411860000     2061.
##  6 2015-12-23 2042. 2065. 2042. 2064. 3484090000     2064.
##  7 2015-12-22 2023. 2043. 2020. 2039. 3520860000     2039.
##  8 2015-12-21 2010. 2023. 2006. 2021. 3760280000     2021.
##  9 2015-12-18 2041. 2041. 2005. 2006. 6683070000     2006.
## 10 2015-12-17 2074. 2076. 2042. 2042. 4327390000     2042.
## # … with 16,597 more rows
## # ℹ Use `print(n = ...)` to see more rows