This week will shift gears and talk about writing reports using markdown via Quarto (the next-gen R Markdown). There are many on-line tutorials for R Markdown (which is compatible with Quarto) which will cover the basics. I’ll focus on some of the aspects that can really speed up your work:
AND today I will show Quarto, which fixes many of the things that were hard with R Markdown reports. Note Quarto is part of RStudio 4.2.1 now; not a separate software that you have to install.
Today is not an intro lecture. I am focusing on big reports with Quarto/R Markdown.
I will be using RStudio Cloud so that you can follow along and you won’t have to install anything.
But when you are ready, upgrade (or have your IT upgrade) RStudio to the latest version. Then do New Project (little arrow in top right) -> New directory -> Quarto xyz (choose any you want to play with). Then click Render and install any necessary packages. Note RStudio now installs {tinytex} (fingers crossed) so rendering to PDF should work.
index.qmd
file. Our first page. Must have this..qmd
files. Our content. Chapters etc._quarto.yml
file. How to put our chapters
together.references.bib
docx: default
to _quarto.yml
I am going to use the visual editor mostly, though I’ll jump to the source code as needed.
In RStudio Cloud, I can easily create Quarto (qmd) documents, but what I am showing today is a Quarto project. I don’t think I can do that (yet) in RStudio Cloud, so let me show you how I created what you see at the hello_quarto_book RStudio Cloud workspace.
references.bib
.Next I pushed this new Git repo up to GitHub and the RVerse GitHub organization. This is my actual work (as opposed to teaching) workflow. * First I added a main branch because RStudio doesn’t do that by default (???). Click on the Git tab, look for the branch icon (diamond with some rectangles), add a branch called main. * Then I switched to GitHub Desktop. added the repo in GitHub Desktop. File > Add local repository. * Now a button to Publish to GitHub pops up (in GitHub Desktop top nav bar) and I clicked that. * Now everything is synced up to GitHub.
Note, I teach the following workflow: Create repo on GitHub and make sure to check the button to add Readme file. Clone that repo onto your computer via GitHub Desktop or RStudio, say. Add your new files to the clone on your computer. Push changes up to GitHub. Why do I teach it this way? It is basically bombproof and the last thing you want when learning to work with Git is tie yourself in knots. It uses only the 3 core Git+GitHub skills that I teach so no need to learn more stuff.
Follow along on RStudio Cloud
Putting raw code for figures and tables in your documents is not a good long-term strategy. Saves time now (maybe) but equals suffering and wasted time later.
Table xyz.Rmd/qmd
, Table abc.Rmd/qmd
. Have
your dedicated markdown wizard figure out the automatic numbering.This used to be horribly and is now easy with Quarto.
For example, we can make a figure with the chunk label
fig-plot
like so. You must use the fig-
prefix.
```{r}
#| label: fig-plot
#| fig-cap: "Plot"
plot(cars)
```
The later in the text we use @fig-plot
to get @fig-plot.
See the Quarto cross-ref page for how to do cross-references for images, figures, and table.
This used to be hard. Now easy with Quarto. Go into visual mode and
type @
and it’ll auto-suggest based on your
.bib
files in your project or any Zotero bibs you have on
your computer. Or you can paste in a DOI and it’ll add that to your
references.
Don’t duplicate chunk labels across your documents.
When using Quarto (or R Markdown), it is best not to use chunk labels in the your Rmd/qmd children unless you need to for figures and tables. It’s too easy to get duplicate labels accidentally.
file.path('figures', 'figure1.Rmd')
here::here('images', 'logo.png')
This way if you have a reference to an image in the
images
folder at the base level of your project, you can
move the document anywhere deep into some folders, and it’ll still find
the image.
Making tables within for
loops is tricky and it is
different if you are outputting to Word versus html and also depends on
what package that you use. See my qmd/Rmd files in the tables folder for
examples of how to set it up.
For many of us, Word is part of our team’s workflow. Here are some tips if that is the case for you:
The example in Table_Counts.Rmd
and
Table_Counts_flex.Rmd
shows you tricks to make nice Word
tables.
format="pandoc"
for the tableresults='asis'
and print()
so you
can use for
loops.kable()
. Use the {flextable}
package if you need that.This is how to get a new page in Word. Make sure you are in print view on the word doc, otherwise you won’t see any of the pages.
```{=openxml}
<w:p><w:r><w:br w:type="page"/></w:r></w:p>
```
Here are some real examples of NOAA Fisheries reproducible reports created with R Markdown.
Northeast US State of the Ecosystem reports, and technical documentation: Website with links to reports, GitHub repo for Mid Atlantic report ❖ GitHub repo for New England report ❖ GitHub tech doc repo ❖ K Bastille paper on the report and process ❖ K Bastille presentation
Atlantic Offshore Wind Development Socioeconomic Reports These reports are all done in R Markdown. You can see how they use a standard format.
{sa4ss} R package for creating stock assessment documents for the Pacific Fishery Management Council. The package provides (1) a consistent structure, (2) generic text that should be the same across all stocks, (3) embedded functionality to create an accessible pdf that satisfies NOAA’s guidance for 508 compliance, and (4) increased speed compared to creating a word document from scratch.
{NMFSReports} R package The NMFSReports Package has all of the basic architecture you need to create reproducible and repeatable NOAA Tech Memos in R Markdown! This approach is perfect for efficiently rolling out annual (or other regular) reports or reports with formulaic sections (the same chapter structure but for a different area or species). Scripts integrate table, figure, data, and bibliography management and design automation.
Fisheries Economics of the United States The 2016-2019 reports were produced with R Markdown. This shows you that you can customize the output of your reports to achieve a specific, and uniform, formating.
The RStudio Cloud workspace (above) is cloned from https://github.com/RVerse-Tutorials/hello_quarto_book
this link. The example qmd files are in https://github.com/RVerse-Tutorials/QmdReport.