r - Capture output in console (nice & tidy) and write it to word file -


i trying export r output table word file. using reporters package this. capture output , pass paragraph. in approach, tidy output gets distorted , doesn't nice in console or when saved text file. how pass output word file? thank in advance.

    data("cars2")     mydoc = docx(title = "summary")     library(gmodels)     aal<-capture.output(crosstable(cars2$country, cars2$type, digits=2, chisq=t))     #capture.output(crosstable(cars2$country, cars2$type, digits=2, chisq=t, format="spss"), file="tests.txt")     mydoc<-addparagraph( mydoc, aal)     writedoc( mydoc, file = "summary.docx") 

you have use monospace font (as r console output using monospace font)

library(gmodels) data(infert, package = "datasets") xx=capture.output(crosstable(infert$education, infert$induced, expected = true, format="spss")) 

solution 1: use existing style (from template) using monospaced font, i.e rrawoutput in default template

library( reporters ) mydoc <- docx(title = "summary") mydoc <- addparagraph( mydoc, xx, stylename = "rrawoutput" ) writedoc( mydoc, file = "summary.docx") 

solution 2: use pot function create piece of text specified monospace font

library( reporters )     mydoc <- docx(title = "summary") mypot <- pot( paste(xx, collapse = "\n"),        format = textproperties(font.family = "courier new", font.size = 9) ) mydoc <- addparagraph( mydoc, mypot,  par.properties = parleft() ) writedoc( mydoc, file = "summary.docx") 

solution 3: 1 not answer question not use gmodels output:

library( reporters ) library( rtable ) library( broom )  data(infert, package = "datasets") myft = freqtable(table(infert$education, infert$induced)) ct = chisq.test(infert$education, infert$induced)  mydoc = docx(title = "summary") mydoc = addtitle(mydoc, "table", level = 2) mydoc = addflextable( mydoc, myft ) mydoc = addtitle(mydoc, "chi-squared test", level = 2) mydoc = addflextable( mydoc, vanilla.table( tidy(ct) ) ) writedoc( mydoc, file = "summary.docx") 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -