knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Start by loading all usual libraries.
library(ClinReport) library(officer) library(flextable) library(dplyr) library(reshape2) library(nlme) library(emmeans) library(car)
Load your data.
# We will use fake data data(datafake) print(head(data))
Create a statistical output for a quantitative response and two explicative variables. For example a treatment group and a time variable corresponding to the visits of a clinical trial.
For that we use the report.quanti()
function:
tab1=report.quanti(data=data,y="y_numeric", x1="GROUP",x2="TIMEPOINT",at.row="TIMEPOINT", subjid="SUBJID") tab1
The at.row
argument is used to space the results between each visit and the subjid
argument is used
to add in the columns header the total number of subjects randomized by treatment group.
Generally we want also the corresponding graphics. So you can use the specific plot function to print the corresponding graphic of your table:
g1=plot(tab1,title="The title that you want to display") print(g1)
You can modify the plot by using the following arguments of the plot.desc()
function:
args(ClinReport:::plot.desc)
Then we can use the report.doc()
function which use the flextable package to format
the output into a flextable
object, ready to export to Microsoft Word
with the officer package.
The table will look like this (we can have a preview in HTML, just to check):
report.doc(tab1,title="Quantitative statistics (2 explicative variables)", colspan.value="Treatment group", init.numbering =T )
The init.numbering
argument is used to specify that
it's the first output. After that, all output numbers
will be increased automatically after each call of the function report.doc()
.
Then, we add those results to a rdocx
object:
doc=read_docx() doc=report.doc(tab1,title="Quantitative statistics (2 explicative variables)", colspan.value="Treatment group",doc=doc,init.numbering=T) doc=body_add_gg(doc, value = g1, style = "centered" )
Write the doc to a docx file:
file=paste(tempfile(),".docx",sep="") print(doc, target =file) #Open it #shell.exec(file)
An example of qualitative statistics with one explicative variables
tab=report.quali(data=data,y="y_logistic", x1="VAR",total=T,subjid="SUBJID") report.doc(tab,title="Qualitative table with two variables", colspan.value="A variable") ``` An example of qualitative statistics with two explicative variables ```r tab=report.quali(data=data,y="y_logistic", x1="GROUP",x2="TIMEPOINT",at.row="TIMEPOINT", total=T,subjid="SUBJID") report.doc(tab,title="Qualitative table with two variables", colspan.value="Treatment group") ``` ### Quantitative tables An example of quantitative statistics with one explicative variables ```r tab=report.quanti(data=data,y="y_numeric", x1="VAR",total=T,subjid="SUBJID") report.doc(tab,title="Quantitative table with one explicative variable", colspan.value="A variable") ``` An example of quantitative statistics with two explicative variables ```r tab=report.quanti(data=data,y="y_numeric", x1="GROUP",x2="TIMEPOINT",at.row="TIMEPOINT", total=T,subjid="SUBJID") report.doc(tab,title="Quantitative table with two explicative variables", colspan.value="Treatment group") ``` ### Mixed Quantitative and Qualitative tables You can mix qualitative and quantitative outputs. But it's only possible for 1 explicative variable, and it should be the same variable for both response: ```r tab1=report.quanti(data=data,y="y_numeric", x1="GROUP",subjid="SUBJID",y.label="Y numeric") tab2=report.quali(data=data,y="y_logistic", x1="GROUP",subjid="SUBJID",y.label="Y logistic") tab3=regroup(tab1,tab2,rbind.label="The label of your choice") report.doc(tab3,title="Mixed Qualitative and Quantitative output", colspan.value="Treatment group")
For the anova table reporting, it's basically a call
to the function xtable_to_flextable()
. The function
report.doc()
just handle the numbering of the output
and the header with the title.
# Removing baseline data for the model data.mod=droplevels(data[data$TIMEPOINT!="D0",]) mod=lme(y_numeric~baseline+GROUP+TIMEPOINT+GROUP*TIMEPOINT, random=~1|SUBJID,data=data.mod,na.action=na.omit) anov3=Anova(mod,3) report.doc(anov3,title="Mixed Qualitative and Quantitative output")
LS-means reporting are based on the package emmeans.
The function report.lsmeans()
enables to format the output:
lsm=emmeans(mod,~GROUP|TIMEPOINT) tab=report.lsmeans(lsm,x1="GROUP",x2="TIMEPOINT",data=data.mod, at.row="TIMEPOINT") report.doc(tab,title="LS-Means example", colspan.value="Treatment Group")
If you want to report contrast, you'll have to specify
contrast=TRUE
in the call to report.lsmeans()
.
contr=contrast(lsm, "trt.vs.ctrl", ref = "A") # Now there is just only one explicative variable # since we make comparison between treatment group # so there is only x1="TIMEPOINT" in the call tab.contr=report.lsmeans(lsm=contr,x1="TIMEPOINT", data=data.mod,contrast=TRUE,at.row="contrast") report.doc(tab.contr,title="LS-Means contrast example", colspan.value="Time points")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.