R : round p values to two significant digits in xtable and ReporteRs export to Latex/Office/LibreOffice -
i using combination of xtable, rtable , reporters export summary tables of various objects (aov, glm's, lm's etc) latex/word/powerpoint. i'm not fond of default way, however, in p values given fixed number of digits after comma. instead prefer give me p values in similar format 1 base r function format.pval() digits=2, i.e. give p values rounded 2 significant digits - in case 4.2e-17 , 0.25).
e.g.
ctl = c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt = c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group = gl(2, 10, 20, labels = c("ctl","trt")) weight = c(ctl, trt) fit = lm(weight ~ group) s=summary(fit) s coefficients: estimate std. error t value pr(>|t|) (intercept) 4.8465 0.1557 31.124 <2e-16 *** group1 0.1855 0.1557 1.191 0.249 library(xtable) library(rtable) library(reporters) tab=as.flextable(xtable(s)) tab 
i have p-values formatted in similar way in summary(fit), (right aligned, rounded 2 significant digits, in case 4.2e-17 , 0.25). idea how achieved in generic way, ideally p values given objects supported xtable?
edit: below made [small package export] helper functions table2doc, table2ppt , table2html export shown r stats object word, powerpoint or html (and function graph2ppt , graph2doc export active graph powerpoint or word), e.g. :
install.packages("rjava") install.packages("reporters") install.packages("reportersjars") install.packages("ggplot2") install.packages("rtable") install.packages("xtable") install.packages("tarifx") install.packages("devtools") library(devtools) devtools::install_github('tomwenseleers/export2office',local=f) library(export2office) ?graph2ppt ?table2doc ## export of ggplot2 plot library(ggplot2) qplot(sepal.length, petal.length, data = iris, color = species, size = petal.width, alpha = i(0.7)) graph2ppt(file="ggplot2 plot.pptx", aspectr=1.7) # add 2nd slide same graph in different aspect ratio graph2ppt(file="ggplot2 plot.pptx", aspectr=1.3, append=t) # add 3d slide same graph fixed width & height graph2ppt(file="ggplot2 plot.pptx", width=6, height=5, append=t) # export of aov anova output fit=aov(yield ~ block + n * p + k, npk) summary(fit) table2doc(file="table_aov.docx") summary(fit) table2doc(file="table_aov.docx",append=t,digits=4) summary(fit) table2doc(file="table_aov.docx",append=t,digits=4,digitspvals=1) summary(fit) table2html(file="table_aov.html") if finds bugs in there let me know, hope post cran later!
define helper function signature of p-value, taking colnames have "pr" in them:
xtable2 <- function(x, ...) { sm <- x[['coefficients']]; ncol <- ncol(sm) whch<- grep("pr", colnames(sm)) digs <- rep(4, ncol+1); digs[whch+1] <- 2 disp <-rep("f", ncol+1); disp[whch+1] <- "g" xtable(x, digits= digs, display=disp, ...) } > tab <- as.flextable(xtable2(s)); tab 
Comments
Post a Comment