Writing up your results

R Script

R script files (.R) are a good starting point for learning to code in R, with simple formatting and the ability to integrate comments with your code. You can easily alternate between code and regular text using comments with a pound sign at the beginning of a line that contains text (#like this). Comments are useful for a number of reasons, including (1) to remind yourself what your code is doing (or denote problems/questions as you work), (2) to explain your code to others, and (3) to provide general organization and clarification throughout your code.

To learn more about R Scripts, visit the Workflow: scripts chapter in R for Data Science.

R Markdown

R Markdown (.Rmd) filed provide a way to present final results with R. Markdown documents can integrate text, code, and output; this combination provides flexibility for presenting your work. R Markdown files Knit into output documents, which are most commonly .pdf (for assignments and “white papers”) and .html (for web content). When working in a Markdown file in RStudio, you can find the Knit button in the tool bar near the top of the file.

Note: One caveat for R Markdown is that sometimes knitting your document can bring up errors that are unrelated to the code itself (in other words, your code might be flawless, and the document still will not knit). This can mean additional troubleshooting, and may be a reason to move your code to an R script while you work.

For more in-depth information on R Markdown see the RMarkdown pages from RStudio.

R Markdown Visual Editor

Writing in an R Markdown document can be challenging, since markdown formatting and syntax can be both frustrating and unfamiliar. To help with this, RStudio has a visual editor for R Markdown documents which feels similar to text editors such as Word or Google Docs. This feature requires RStudio version 1.4 or higher, so you may need to update RStudio if the R Markdown visual editor is not available to you.

The visual editor removes the need for remembering and using markdown formatting expressions, and still supports markdown expressions as well as traditional shortcuts and formatting icons. Visual mode also includes support for integration with Zotero for citation assistance.

To use the visual editor, open a new or existing R Markdown file and click the button, located in the top right corner of the document toolbar. Alternatively, locate the gear icon next to the Knit button, and select Use Visual Editor from the resulting dropdown menu. Both of these methods can be used to switch between the default and visual editors.

For more information on the features available through the visual editor, check out RStudio’s official Visual RMarkdown documentation page.