Skip to content
R for the Rest of Us Logo

Tips for Working with Quarto

If the video is not playing correctly, you can watch it in a new window

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Your Turn

  1. Working in your report.qmd file, right below the YAML, add a code chunk that loads the tidyverse and imports your penguins data to an object called penguins.

  2. Add a first-level heading with the text Results.

  3. Right below the Results heading, create a second code chunk and add some code that you used to make a graph in the data viz section of the course.

  4. Render your document and make sure it works!

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Brian Slattery

Brian Slattery

October 4, 2023

For the third tip, I wasn't sure if there was a way to source R script documents in a Quarto document? I tried googling this and it looked like there was a source() function that lets R script files reference other R script files, but it seemed like there were some limitations too. I wasn't sure how Quarto documents differed from R script documents in that way. Thanks!

Gracielle Higino

Gracielle Higino Coach

October 4, 2023

Hey, Brian! For this tip you don't need to source - copy+paste is enough! - but you can do that for sure! It's a great thing to learn. I'm not aware of the limitations of sourcing, it has always worked fine for me, but I know you need to write your R script in a way that is independent (i.e., it must be able to run alone or it has a very clear chain of execution with other scripts). For rendering Quarto documents, you should add local = knitr::knit_global() as an argument of your source() function. For example, if I want to source an R script that creates an object that stores a plot called p, I could add this to my code chunk:

source("my_beautiful_plot.R", local = knitr::knit_global())    #sources the code that creates the object
p    # displays the object

Quarto is a way to render Pandoc Markdown markup language, which means it is a way to control structure, format and relationships in a document. R scripts are a "roadmap" of functions to execute that will only result in more data or plots. When we add a code chunk in a *.qmd file, we are adding a relationship between an R script and other elements of the document.

I hope that's helpful!

Leo Gutknecht-Gmeiner

Leo Gutknecht-Gmeiner

April 3, 2024

  1. When loading packages and data: can I do this at the top of the document so that it will be there throughout?
  2. Can I have the RScriptfile where I have my code open side-to-side with the Quarto Document - this would enable me to copy + paste easily. Otherwise this might be very tedious....

Leo Gutknecht-Gmeiner

Leo Gutknecht-Gmeiner

April 3, 2024

Ad 2. I tried it: the RScript opens up next to the Quarto-file on the lefthand upper pane. So there is a possibility of switching between the two.

Libby Heeren

Libby Heeren Coach

April 3, 2024

Hey, Leo! You can also add another "source" pane so that you have them side by side. In RStudio, go to the "View" menu at the top and select Panes > Add Source Column.

Gracielle Higino

Gracielle Higino Coach

April 3, 2024

Hi Leo! That's a good thing to do! I'd only recommend that you load your data somewhere else in your document if you have heavy things to run/load before you actually get to the point of using the data, but overall this doesn't really matter. By loading your data and packages upfront, you guarantee you have all that you need from "outside" of your code right in the beginning, and it gets easier to catch a problem right away.

Leo Gutknecht-Gmeiner

Leo Gutknecht-Gmeiner

April 4, 2024

That's handy. Thanks for the tip:)

Leo Gutknecht-Gmeiner

Leo Gutknecht-Gmeiner

April 4, 2024

Thanks Gracielle. But one does not put the loading of the data and the necessary packages into the YAML (that would be my intuitive way of doing things), but afterwards? Is this understanding correct?

Gracielle Higino

Gracielle Higino Coach

April 4, 2024

Yes! Packages and data should go into a code chunk right after the YAML. The YAML is a special type of “code” where we put parameters related to the rendering of the document, not the analysis within it. [=

Leo Gutknecht-Gmeiner

Leo Gutknecht-Gmeiner

April 4, 2024

Thanks, that's helpful