2023-11-07
Quarto is a computer program that allows you to generate documents in various formats with code
We will use it to generate MS Word documents with R
It can also generate PDF, HTML, and other output
It can also use other programming languages like Python
Part of data analysis is communicating your results to an audience
For example, writing a final report for a class, or giving a presentation at work
You should have already installed Quarto before class
If not, please go to https://quarto.org/docs/get-started/ and install it now
We will continue to use the gapminder-analysis
project in the data-analysis-course
folder on your Desktop
Navigate there and click on gapminder-analysis.Rproj
Create a new Quarto file by clicking File > New File > Quarto Document…
In menu that appears, type "Gapminder Analysis"
for the title and your name for the author
Leave format as HTML (as it says, we can change it at any time later)
RStudio will create a file. Save it as "gapminder-report.qmd"
(.qmd
is the file extension for Quarto files)
Look at your new Quarto file. What do you notice that is different from the .R
files we have been using so far?
There is normal text like “Quarto enables you to weave together content…” that does not appear in a comment
There are lines starting with two hashes (##
)
There are lines like ```{r}
What is going on here?
A Quarto (.qmd
) file is set up opposite to an R script
Normal lines of text are not code (so they don’t need a #
like a comment)
R code is contained within lines starting with ```{r}
and ending with ```
(called a “code chunk”)
Another important difference between normal .R
files and .qmd
files is that the latter produce documents
This is called “rendering”
Let’s try that now: press the “Render” button in RStudio
You should see the rendered document (a web page, in this case) appear on the right side of RStudio (in the Files and Plots panel)
gapminder-report.html
fileNotice the formatting of the output: lines like ## Quarto
appear as headers
You can also see the output of the code, like 1 + 1
The .qmd
file is written in plain text (without formatting, similar to an .R
file)
We can specify formatting in the output using Markdown syntax
As you can see in the document, the hash symbol (#
) is used to define headings, which appear larger than other text
#
Heading level 1 (biggest)##
Heading level 2###
Heading level 3…For italics, wrap the text in *
, like this *this*
(renders like this)
For bold, wrap the text in **
, like this **this**
(renders like this)
.qmd
files are known as “Quarto Markdown” because they add Quarto’s features (the ability to include code chunks) to Markdown
You may have heard of “R Markdown,” which is the precursor to Quarto Markdown
RStudio has a handy way for you to preview what the rendered text will look like
Right now we are in “Source” mode, which means we are editing the plain text
Click on the “Visual” mode button to switch to “Visual” mode
Now it looks much more like the output on the right
I much prefer working with plain text, so we will switch back to “Source” mode
But it is a good idea to use “Visual” mode to check what your markdown formatting will look like
There are many guides online to learn more about writing Markdown
#
tidyverse
package, then read in the gapminder
dataset like we did beforeNotice that when we render the document, the gapminder dataset is not loaded into our current R session
That is because rending takes place in a separate instance of R
To run the R code in a chunk in your current R session, press the green “play” button at the right-hand side of the chunk
In addition to code chunks, you can also mix short bits of R code within normal text by first writing a backtick (`
) and the letter r
, then the R code, then another backtick
For example, we can show the number of rows in the dataset with `r nrow(gapminder)`
This is best used for describing facts about the data or results
variable: value
, for example author: "Joel Nitta"
Similar to the YAML header, we can also specify settings for the code chunks
This is done by first writing #|
, then the setting like you would for the YAML header
For example, in a report, you usually only want to show the results of the code, not the code itself
You can do this with the following code chunk option: #| echo: false
(remember echo
echoes the code)
message: false
Hide code messageswarning: false
Hide code warningsSo far, we have been using HTML output because it is the default and shows up immediately when we press “render”
But your report should be submitted in Word (.docx) format.
Change the output format to docx
and render the document again
.gitignore
:
gapminder-report.docx
gapminder-report.html
gapminder-report_files
(files needed for HTML output)*.docx
Quarto allows you to insert your analysis results directly into reports
Quarto can generate multiple output formats
The way of writing code and plain text is reversed compared to R scripts
Please try to use Quarto for your class reports!
I have created an example report for you using the gapminder dataset: https://github.com/data-analysis-chiba-2023/gapminder-report
You can look at the .qmd
file online, or clone the repo to your computer
Clone the project on GitHub by clicking on the link in Moodle
Be sure to enter your team name correctly! (pay attention to lower and upper case)
This time, all team members have access to the same repository
report.qmd
file. Edit it to match your dataset and team members.
To avoid merge conflicts (incompatible changes to the same file made by different people) make a copy of the final report file for each team member to work on.
report-jn.Qmd
Share your work by pushing and pulling
When you are ready to submit the final report, one person should edit the final report file report.Qmd
Also submit the repo URL on Moodle as usual
The report (report.qmd
) is due 2023-11-20 11:59 PM
The presentation will be given in class on 2023-11-21