Markdown documents contain simple codes that format the output, with applications in a variety of settings. Set up and run R Markdown files, filetype .Rmd, to create dynamic web (HTML), MS Word, or pdf output files. The output of a markup document blends text with R or Python (or other) code to dynamically create statistical output with your interpretative text.
To get started, open RStudio. In this example there are no .R or .Rmd files present when RStudio is opened. There is no problem if other files have already been opened for other situations, but no need either.
Open RStudio with no input files present.
Create the R markdown document within RStudio. Do so by going to the File memu and select New File, then R Markdown… . Or, click on the + sign at the top-left cornder of the RStudio window and select R Markdown… .
Create an R Markdown file.
The following dialogue appears. Enter the title of the document and choose the form of output. HTML (web page) is default.
Title and type of document.
Title the document,
Enter the title.
RStudio provides an example document when you create a new markdown document. It is just about self-explanatory. A markdown document consists of code blocks, R code in this example, and explanatory text. When processed, the markdown documents formats the text and runs the code, displaying the output by default.
Rmd Document, Beginning.
Each code block can be displayed on the output, or not. The output of each code block can be displayed, or not. Choose the “gear menu” located on the right side of each code block to select the desired option, or just enter directly.
Type of interaction with the output.
Output of each code block includes text as well as visualizations.
Rmd document, End.
Generate the output with the Knit button in RStudio (or a console function call if not). The document is automatically saved when knit.
Knit the document, that is, generate the output, here an html file.
Help is available. There is more to know, but this provides most or all of what you need to markup documents, such as headers, italics, images, etc. Choose the RStudio Help menu, then select Markdown Quick Reference.
Get R Markdown help.
Here is some of the help output, which appears in the lower-right window pane.
Example of help output.
You can create a code block by entering the defining characters, or choose the Insert menu at the top of the window pane for the .Rmd file, and then choose the language, here R.
Generate new R code block.
Here is the new code block, ready for entered R code. Enter as many lines as you wish in any one code block, one or many lines.
The R code block.
To run Python code as part of an R Markdown document, first invoke the reticulate library, which is R code, and hence contained within an R code block.
Note that the use_python() function path is a Macintosh (Unix) path, needed to identify the Anaconda installed Python as the Python to use, not the older version of Python installed with the Mac OS. This function is generally not needed running Windows.
Generate Python code block.
The following provides the minimal R code needed to link to Python, and then provides a brief example of Python code, reading data into a data frame. Python code is contained within Python code blocks.
Example Python code.