Quarto HTML Exercise

Note

You can click Preview in the upper left corner of the Editor window at any time. If you save the file after making changes and refresh your browser, you’ll see the changes.

  1. Create a new R project in Positron. Put it in a folder called “TryQuarto”.

File -> New Folder from Template

  1. Add a new .qmd document called “Theoph.qmd”
  2. Using markdown, annotate your code with these level 2 headings: Analysis, Plot
  3. Before the Analysis section, insert an R code chunk to load the tidyverse metalibrary and flextable library. Install these packages using install.packages() if you don’t have them already. Use the code chunk options to hide the code and messages from the output.
#| label: setup
#| echo: false
#| eval: true
#| message: false
  1. In the analysis section, explain in markdown that you are going to first create a new data frame from Theoph called trough.
  2. Insert an R code chunk. In it use a tidyverse pipe if you can, to create trough. Label it “create-trough”.
  • #| label: create-trough
  • Use dplyr::group_by to group by Subject
  • Create new variables with dplyr::summarize where wt and dose are the first records for Wt and Dose, time is the maximum of Time, and conc is the concentration which occurs at time.
  • Create new variables with dplyr::mutate where id is the numeric form of the character form of Theoph$Subject and dosekg is dose/wt.
  • Remove the grouping with dplyr::ungroup.
  • Use dplyr::arrange to sort by id
Click to reveal an answer
trough <- 
  Theoph |> 
  group_by(Subject) |>
  summarize(
    wt = Wt[1],
    dose = Dose[1],
    time = max(Time),
    conc = conc[which(Time == time)]
  ) |>
  mutate(
    id = as.numeric(as.character(Subject)),
    dosekg = dose / wt
  ) |>
  ungroup() |>
  arrange(id)
  1. What happens if you add #| echo: false or #| eval: false to the code chunk?
  2. Add markdown to indicate you are examining how well dose predicts trough.
  3. In the next R chunk labeled “dose-lm” with echoing and evaluation, use the stats::lm function to create a linear regression called lm_dose of trough$dose as a predictor of trough$conc. Include a summary of lm_dose in your code chunk.
  • ?lm
  • lm_dose <- lm(...)
Click to reveal the answer
lm_dose <- lm(conc ~ dose, data = trough)
summary(lm_dose)
  1. Add more markdown to indicate you are now going to check if dosekg is a better predictor of the trough concentration. Bold important parts of your text.
  2. Add another R chunk with no echoing labeled “dosekg-lm” to create a linear regression called lm_dosekg of trough$dosekg as a predictor of trough$conc.
  3. Add an R chunk with both echo and evaluation to summarize lm_dosekg.
  4. After the level 2 “Plot” heading, visualize both regressions with ggplot, geom_point, and geom_smooth.

trough |> ggplot(aes( x = ? , y = ?)) + geom_point() + geom_smooth(method = "lm")

Click to reveal the answer
trough |> ggplot(aes( x = dose, y = conc)) + geom_point() + geom_smooth(method = "lm")
  1. Write a conclusion in markdown after another level 2 heading.
  2. Find Quarto: Render Document in the Command Palette and click it. This will render your document to HTML. You can also use the keyboard shortcut Ctrl + Shift + K (Windows) or Cmd + Shift + K (Mac).