Creating PDF files using R markdown
Paul Northrop
Source:vignettes/stat0002-creating-pdf-files-vignette.Rmd
stat0002-creating-pdf-files-vignette.Rmd
When working on STAT0002 Exercises 1 you have the option of using R to perform calculations and create plots. If you prefer to do everything by hand then follow the guidance in the Creating your PDF file section of Exercise submission and your participation mark in the Exercises section on the STAT0002 Moodle page.
This article describes ways to use R to create a PDF file containing your work. In the long-term using R Markdown is arguably the best option for creating documents that contain R output. If you are taking STAT0004, or auditing the STAT0004 Moodle page, then R Markdown will be covered towards the end of Term 1, so you could wait until then to try R Markdown and, for now, use the following “copy-and-paste” approach.
Copy-and-paste
This is the least elegant approach, but it is simple, at least in principle.
- Open a file in the word processor of your choice. (It must have the
capability of creating a PDF file.)
- For the weekly exercises include a title like “Exercises 1” and your full name and student ID number.
- Type text to answer questions in the usual way.
- R code with output in the R Console:
- Highlight the code and the output, e.g. using left click with a mouse.
- Copy the highlighted text e.g. right click and Copy.
- Navigate to your word processor file and paste the text where you would like it to appear, e.g. right click Paste.
- R code producing plot output in the Plots tab. Either:
- Click “Export” and “Save image as …”, save in the format of your choice and import the image file into your document, or
- Click “Export” above the plot, “Copy to Clipboard”, choose “Bitmap” or “Metafile”, and paste into our document.
When you are finished, save your file in PDF format.
In part 4, you may find that you need to spend time learning which file format work best for you and how to change the size and position of plots in your document. To be honest, I do not know how best to do this because I never use the copy-and-paste approach.
R Markdown in RStudio
I will assume that you are using RStudio. You might find R Markdown: The Definitive Guide helpful.
I have provided a template R Markdown file for you to use.
- Follow the link ex1template.Rmd, click on the “Download raw file” (a downwards pointing arrow) icon near the top right of the page.
- You may like to rename this file. If you do this then replace
ex1template
in the text below with the name that you chose. - Move this file to a directory of your choice and click on it to open a new RStudio session.
The file should open in RStudio, if you have RStudio installed.
There are several ways to create a PDF using from an
.Rmd
file. The easiest way is to use the Knit button, but I
will also outline approaches that call an R function from the command
prompt >
in the RStudio Console.
Using the Knit button
Above the title “Template Rmd document for Exercises 1” is a Knit button with a triangle on its right side.
Knit to Word
- Click on the triangle and select “Knit to Word”. A file
ex1template.docx
is created in your chosen directory. - If you have software that enables you to open
ex1template.docx
then you can save this file in PDF format.
Knit to HTML
- If you select “Knit to HTML” then a file
ex1template.html
is created. - You can create a PDF file from
ex1template.html
by opening it in a browser, e.g. Google chrome, and printing to PDF.
Knit to PDF
It is possible to Knit directly to PDF format by selecting the “Knit to PDF” option. However, this requires that an installation of LaTeX - software designed to create nice-looking mathematics - is available to RStudio on the computer that you are using. You may never heard of LaTeX, and might not need it unless you do a final year project.
However, if “Knit to PDF” does not work and you would like it to,
then follow the instructions in Chapter 1
Installation of R
Markdown: The Definitive Guide for creating a LaTeX installation
using TinyTeX
.
Using an R function
The Knit button creates output of different types by calling an R
function, in the rmarkdown
package, called
render
. The following code will work provided that your Rmd
file is located in your current working directory, which will be the
case if you started your RStudio session by clicking on your Rmd file to
open it. To check this, use the getwd
function:
The accessr
package
The accessr
package provides functions that call render
for certain
output file formats. The following command line R code creates Word and
HTML documents. The Word document will look a bit different to the one
produced above because accessr
uses a different default
style of Word document.
If you are using Microsoft Windows then you can set
pdf = TRUE
to create automatically a PDF copy of an output
Word document. This requires the OfficeToPDF
software to be installed.
> # Install OfficeToPDF from https://github.com/cognidox/OfficeToPDF/releases
> install_otp(dir = "accessr")
> # Knit and render to Word and PDF
> # (Microsoft Windows only)
> rmd2word("ex1template", pdf = TRUE)
Similarly, on Windows, macOS or linux, we can print HTML output to PDF automatically.