Skip to content

Instantly share code, notes, and snippets.

@davidgohel
Created June 29, 2016 16:06
Show Gist options
  • Save davidgohel/323554af8df45c5bf83213f22d647fb6 to your computer and use it in GitHub Desktop.
Save davidgohel/323554af8df45c5bf83213f22d647fb6 to your computer and use it in GitHub Desktop.
library(boot)
library(dplyr)
library(magrittr)
library(purrr)
library(ReporteRs)
# get some data -----
data(melanoma)
melanoma$Status = c( 'Melanoma', 'Alive' , 'Non-melanoma' )[melanoma$status]
melanoma$Gender = ifelse( melanoma$sex > 0, 'Male', 'Female' )
melanoma$Ulceration = ifelse( melanoma$ulcer > 0, 'Present', 'Absent' )
data = melanoma %>%
group_by( Status, Ulceration ) %>%
summarize( Mean = round( mean( thickness, na.rm = T ), 3 ),
SD = round( sd( thickness, na.rm = T ), 3 ),
Median = median( thickness, na.rm = T )
)
data = as.data.frame( data )
# pain to get the data.frame in a correct format -----
list_datas <- split(data, data$Status) %>% map( function(x){
x[,-1]
})
newdata <- group_by(data, Status) %>% do(head(.,n=1)) %>% ungroup()
list_titles <- split(newdata, newdata$Status)
newdata <- map2_df(list_datas, list_titles, function(x, z) bind_rows(z, x))
# Start creating the table
mytable1 <- vanilla.table(newdata) %>%
spanFlexTableColumns(i = which(!is.na(newdata$Status)), from = 1, to = ncol(newdata) ) %>%
setFlexTableWidths(widths = c( .7, rep(1,ncol(newdata)-1)))
mytable1[which(!is.na(newdata$Status)),] <- textBold()
mytable1[] <- parCenter(padding=2)
mytable1[, to = "header"] <- parCenter(padding=2)
mytable1[,1] <- parLeft(padding=2)
# print in the viewer
print( mytable1 )
# print in test.docx
docx() %>%
addFlexTable(mytable1) %>%
writeDoc(file = "test.docx")
@davidgohel
Copy link
Author

shot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment