Skip to main content

Working with R and Word

Are you using R for data analysis and then writing up your results in Word? This is a common workflow, but can be tedious if you’re frequently updating your analysis and need to copy your results from R into Word. A manual process can also lead to errors, and result in your paper being out of date, undermining the good work you’re doing making your analysis reproducible by using R.

Fortunately, there are several solutions to make getting R results into your Word document easier.

Approaches

StatTag

StatTag is developed by a team at Northwestern.  It allows you to insert “tags” directly into a Word document that execute R code and return the result.  The output is integrated into the Word document.

Advantages:

  • You can format and edit your Word document like any other Word document.  This is particularly useful if you’re submitting to a journal or writing a dissertation where there are formatting requirements.
  • Comments and other review markup in Word are unaffected, so you can continue to collaborate with others.  You can share the document with others who do not have StatTag installed themselves.

Considerations:

  • StatTag must be installed on your laptop, and you need to work with the desktop version of Word instead of the online version.
  • Creating the tags in the document can take some time and work.
  • If you have analyses that take a long time to run, you may want to consider other workflows or need to preprocess datasets to create intermediate files that you can use with StatTag instead of computing everything from scratch.
  • While you can edit the R code files from within Word/StatTag, it’s best to write the code initially in R and make any large changes there.

R Markdown to Word

R Markdown can compile documents directly to Word format.  R Markdown is an alternative to an R script that you write in RStudio with a combination of a little bit of special formatting, plain text written with Markdown formatting, and “chunks” of R code that can be executed and produce output.

Advantages:

  • Provides a single document where you can easily see and edit both text and R code together without linking multiple documents or working with R embedded in a special tag in Word.
  • A good option for quickly getting output in a Word document to send to others for review when Word is the requested format.
  • Can include both R code and output in the Word document, or just the output of the R code.
  • The same R Markdown file can (sometimes with slight modification) be used to create HTML or PDF output.
  • Instead of producing an entire paper in R Markdown, it can also be useful for producing just tables or other individual pieces of output that could then be more easily copied into a main Word document than copying R output from the console.

Considerations:

  • If you need your Word document to be in a particular format or follow particular style guidelines, it can be tedious to get everything just right from R Markdown.  You can use Word templates, and the officedown package facilitates some formatting, but both options take patience and may not be full solutions.
  • The Word document you produce from R Markdown is a standalone document that you can send to others.  They can review and edit as they would other Word documents.  There is not, however, a mechanism for integrating changes back into the original R Markdown file.  Each time you change the R Markdown file, you recompile and produce a new Word document.

A Minimal Solution

If you aren’t ready to implement one of the above solutions, which can take time to learn and set up, there are still things you can do to improve your workflow.  Start by creating a single script or R Markdown file that produces all of the output you need to include in the paper.  When you update something with the data or analysis, re-run the entire script to ensure you have all of the current information.

Further tips:

  • Keep the analysis script in the order that the information appears in the paper to make checking the correspondence between the analysis output and the write-up easier.
  • If you’re using R Markdown, the images will appear in the output, but make sure to also save the files to .png file types so that you can include them in the paper.  Explicitly set the width and height of the image files you save to make sure they are consistent and the fonts are scaled appropriately for the size of the plot in the paper.
  • Consider including in the output sentences that contain the results you need so you can copy and paste them directly.  For example: “X shows a positive relationship with Y, with an estimated coefficient of 0.123 (p-value=0.02).” Inline R code can help you write such sentences in R Markdown documents.

Additional Tips: Tables

Tables can be particularly time consuming to create.  There are some R packages that will help with that process.  The packages referenced below can all produce at least HTML output (easiest to do from an R Markdown document), and HTML output can be copied into and manipulated relatively easily with Word.

If you need to include a table summarizing output from a statistical model, especially a regression-based model, there are packages to help you do that.  Start with stargazer, but if it doesn’t do what you need, there are alternatives.

The easystats report package will even generate boilerplate text summarizing the results of your statistical tests for you.

gtsummary is particularly useful for generating the type of tables expected in clinical and health sciences fields.  This includes both summaries of the datasets and regression model output.

Getting Help

The Research Data Services team includes staff and student consultants who would be happy to help you work through specific issues that come up when trying to implement these solutions.  Let us know how we can help by requesting a free consultation.  We’ll get back to you via email and can set up a virtual meeting.