Best ways of getting tables from R into Libre Office Writer?

2.2k views Asked by At

Currently, my work flow is that I do statistics and data restructuring in R and write my documents using Libre Office (LO) Writer. The results from the analyses in R are often tables and I need to get these into LO Writer. I have not found a way to do this directly in an easy way, so what I normally do is: 1) put together the table in R, 2) export table as .csv, 3) open .csv in LO Calc, 4) copy from LO Calc to LO writer using paste special as RTF.

When saving to .csv, I am doing this by using write.csv(table_1, "table_1.csv", na = ""). This results in an extra step because LO Calc needs to know how to read the .csv file. Alternatively, one can install one of the packages for outputting to .ods or .xls(x).

One problem doing it this way is that this frequently results in very long decimal numbers in the file, e.g. 2.21931, whereas one typically only wants to show 2 or 3 digits in the document. I have found two solutions for this annoyance.

The first is to round the numbers in R using e.g. round(table_1, 2) and then saving using the above command. The problem with this is that when one imports this into LO Calc, numbers like .60 will become .6, losing a (redundant) digit. This makes for a somewhat inconsistent presentation. One can add them back either in the table in the document or by using the rounding functions in LO Calc.

The second is to use the rounding function in Libre Office Calc, but this requires a few extra works/more work.

Finally, when important tables into LO Writer like this, the table will have invisible borders. One will have to add these manually for every table.

What is the best way to get result tables from R into LO Writer while avoiding the significant digits problem and extra clicks?

2

There are 2 answers

0
Richie Cotton On

If you want a completely automated way of generating LibreOffice documents, the standard R way of doing things is to write your document using knitr, then convert the resulting file into ODT format using Pandoc.

(Pandoc doesn't yet support AsciiDoc as an input format, so you are limited to writing your document in markdown/LaTeX/HTML/textile/reStructuredText.)

0
Aaron Katch On

Try doing your formatting in R with functions like sprintf(). It's the best way I've found to control display of digits. The url below has good working examples.

http://www.cookbook-r.com/Strings/Creating_strings_from_variables/

Also, consider the knitr package to create documents on the fly.