Quarto edits (#1344)

* Quarto edits

* Add exercise
This commit is contained in:
Mine Cetinkaya-Rundel 2023-03-07 08:48:34 -05:00 committed by GitHub
parent 7b192c3447
commit dfb8b5602b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 18 deletions

View File

@ -149,11 +149,13 @@ While Markdown is designed to be easy to read and write, as you will see in @sec
Therefore, if you're new to computational documents like `.qmd` files but have experience using tools like Google Docs or MS Word, the easiest way to get started with Quarto in RStudio is the visual editor.
In the visual editor you can either use the buttons on the menu bar to insert images, tables, cross-references, etc. or you can use the catch-all <kbd>⌘ /</kbd> shortcut to insert just about anything.
If you are at the beginning of a line (as shown below), you can also enter just <kbd>/</kbd> to invoke the shortcut.
If you are at the beginning of a line (as shown in @fig-visual-editor), you can also enter just <kbd>/</kbd> to invoke the shortcut.
```{r}
#| echo: false
#| label: fig-visual-editor
#| out-width: "75%"
#| fit-cap: |
#| Quarto visual editor.
#| fig-alt: |
#| A Quarto document displaying various features of the
#| visual editor such as text formatting (italic, bold,
@ -180,7 +182,13 @@ Most importantly, while the visual editor displays your content with formatting,
### Exercises
<!--# TO DO: Add exercises. -->
1. Re-create the document in @fig-visual-editor using the visual editor.
2. Using the visual editor, insert a code chunk using the Insert menu and then the insert anything tool.
3. Using the visual editor, figure out how to:
a. Add a footnote.
b. Add a horizontal rule.
c. Add a block quote.
4. In the visual editor, go to Insert \> Citation and insert a citation to the paper titled [Welcome to the Tidyverse](https://joss.theoj.org/papers/10.21105/joss.01686) using its DOI (digital object identifier), which is 10.21105/joss.01686. Render the document and observe how the reference shows up in the document. What change do you observe in the YAML of your document?
## Source editor {#sec-source-editor}
@ -208,21 +216,19 @@ If you forget, you can get to a handy reference sheet with *Help \> Markdown Qui
Each of the sections should include a bulleted list of jobs/degrees.
Highlight the year in bold.
2. Using the visual editor, figure out how to:
2. Using the source editor and the Markdown quick reference, figure out how to:
a. Add a footnote.
b. Add a horizontal rule.
c. Add a block quote.
3. Now, using the source editor and the Markdown quick reference, figure out how to:
a. Add a footnote.
b. Add a horizontal rule.
c. Add a block quote.
4. Copy and paste the contents of `diamond-sizes.qmd` from <https://github.com/hadley/r4ds/tree/main/quarto> in to a local R Quarto document.
3. Copy and paste the contents of `diamond-sizes.qmd` from <https://github.com/hadley/r4ds/tree/main/quarto> in to a local R Quarto document.
Check that you can run it, then add text after the frequency polygon that describes its most striking features.
4. Create a document in a Google doc or MS Word (or locate a document you have created previously) with some content in it such as headings, hyperlinks, formatted text, etc.
Copy the contents of this document and paste it into a Quarto document in the visual editor.
Then, switch over to the source editor and inspect the source code.
## Code chunks
To run code inside a Quarto document, you need to insert a chunk.
@ -473,7 +479,7 @@ For example, if your default `fig-width` is 6 and `out-width` is 0.7, when you s
### Other important options
When mingling code and text, like in this book, you can set `fig-show: "hold"` so that plots are shown after the code.
When mingling code and text, like in this book, you can set `fig-show: hold` so that plots are shown after the code.
This has the pleasant side effect of forcing you to break up large blocks of code with their explanations.
To add a caption to the plot, use `fig-cap`.
@ -486,11 +492,20 @@ In that case, set `fig-format: "png"` to force the use of PNGs.
They are slightly lower quality, but will be much more compact.
It's a good idea to name code chunks that produce figures, even if you don't routinely label other chunks.
The chunk label is used to generate the file name of the graphic on disk, so naming your chunks makes it much easier to pick out plots and reuse in other circumstances (i.e. if you want to quickly drop a single plot into an email or a tweet).
The chunk label is used to generate the file name of the graphic on disk, so naming your chunks makes it much easier to pick out plots and reuse in other circumstances (i.e. if you want to quickly drop a single plot into an email).
### Exercises
<!--# TO DO: Add exercises -->
1. Open `diamond-sizes.qmd` in the visual editor, find an image of a diamond, copy it, and paste it into the document. Double click on the image and add a caption. Resize the image and render your document. Observe how the image is saved in your current working directory.
2. Edit the label of the code chunk in `diamond-sizes.qmd` that generates a plot to start with the suffix `fig-` and add a caption to the figure with the chunk option `fig-cap`. Then, edit the text above the code chunk to add a cross-reference to the figure with Insert \> Cross Reference.
3. Change the size of the figure with the following chunk options, one at a time, render your document, and describe how the figure changes.
a. `fig-width: 10`
b. `fig-height: 3`
c. `out-width: "100%"`
d. `out-width: "20%"`
## Tables
@ -520,7 +535,9 @@ Each provides a set of tools for returning formatted tables from R code.
### Exercises
<!--# TO DO: Add exercises -->
1. Open `diamond-sizes.qmd` in the visual editor, insert a code chunk, and add a table with `knitr::kable()` that shows the first 5 rows of the `diamonds` data frame.
2. Display the same table with `gt::gt()` instead.
3. Add a chunk label that starts with the suffix `tbl-` and add a caption to the table with the chunk option `tbl-cap`. Then, edit the text above the code chunk to add a cross-reference to the table with Insert \> Cross Reference.
## Caching {#sec-caching}
@ -598,16 +615,16 @@ Then you can write:
rawdata <- readr::read_csv("a_very_large_file.csv")
`r chunk`
As your caching strategies get progressively more complicated, it's a good idea to regularly clear out all your caches with `knitr::clean_cache()`.
We've followed the advice of [David Robinson](https://twitter.com/drob/status/738786604731490304) to name these chunks: each chunk is named after the primary object that it creates.
This makes it easier to understand the `dependson` specification.
As your caching strategies get progressively more complicated, it's a good idea to regularly clear out all your caches with `knitr::clean_cache()`.
### Exercises
1. Set up a network of chunks where `d` depends on `c` and `b`, and both `b` and `c` depend on `a`. Have each chunk print `lubridate::now()`, set `cache: true`, then verify your understanding of caching.
> > > > > > > 7ff2b1502187f15a978d74f59a88534fa6f1012e \## Troubleshooting
## Troubleshooting
Troubleshooting Quarto documents can be challenging because you are no longer in an interactive R environment, and you will need to learn some new tricks.
Additionally, the error could be due to issues with the Quarto document itself or due to the R code in the Quarto document.