From 5d574f2d8e798582317a8382a17e12189ada9a03 Mon Sep 17 00:00:00 2001 From: awunderground Date: Wed, 25 Sep 2024 15:00:07 -0400 Subject: [PATCH] Update putting it all together notes --- docs/01_lesson.html | 2 +- docs/02_lesson.html | 30 +- docs/03_lesson.html | 34 +- docs/04_lesson.html | 6 +- docs/05_lesson.html | 8 +- docs/06_lesson.html | 343 +++++++++++++----- docs/index.html | 6 +- docs/search.json | 53 ++- docs/www/images/inline-r-code.png | Bin 0 -> 17552 bytes docs/www/images/knuth.jpg | Bin 0 -> 15542 bytes docs/www/images/recipe.png | Bin 0 -> 45110 bytes docs/www/images/render.png | Bin 0 -> 8610 bytes intro_series_book/06_lesson.qmd | 209 ++++++++--- .../www/images/inline-r-code.png | Bin 0 -> 17552 bytes intro_series_book/www/images/knuth.jpg | Bin 0 -> 15542 bytes intro_series_book/www/images/recipe.png | Bin 0 -> 45110 bytes intro_series_book/www/images/render.png | Bin 0 -> 8610 bytes urbn101-intro-r.Rproj | 13 + 18 files changed, 497 insertions(+), 207 deletions(-) create mode 100644 docs/www/images/inline-r-code.png create mode 100644 docs/www/images/knuth.jpg create mode 100644 docs/www/images/recipe.png create mode 100644 docs/www/images/render.png create mode 100644 intro_series_book/www/images/inline-r-code.png create mode 100644 intro_series_book/www/images/knuth.jpg create mode 100644 intro_series_book/www/images/recipe.png create mode 100644 intro_series_book/www/images/render.png create mode 100644 urbn101-intro-r.Rproj diff --git a/docs/01_lesson.html b/docs/01_lesson.html index 9a9afad..9e17709 100644 --- a/docs/01_lesson.html +++ b/docs/01_lesson.html @@ -140,7 +140,7 @@ diff --git a/docs/02_lesson.html b/docs/02_lesson.html index 8018ce6..f03d703 100644 --- a/docs/02_lesson.html +++ b/docs/02_lesson.html @@ -7,7 +7,7 @@ - + Intro to R - 2  Data Munging 1 @@ -76,7 +110,7 @@ - + +
+

+Call:
+lm(formula = dist ~ speed, data = cars)
+
+Coefficients:
+(Intercept)        speed  
+    -17.579        3.932  
-

Source: RStudio

- +

An increase in travel speed of one mile per hour is associated with a 3.93 foot increase in stopping distance on average.

-
-

6.4 urbntemplates

-

library(urbntemplates) contains three families of functions:

- -

A sensible workflow is: 1. Start a new project and create a .Rproj by submitting urbntemplates::start_project(). This will create and open a new .Rproj. 2. Inside the .Rproj, add the necessary documents for a part of a project, like a Shiny application, with a construct_() function. 3. Add any desired remaining templates or documents with use_() functions.

-
-

6.4.0.1 start function

-
    -
  • start_project()
  • -
+
+

6.4.2 Quarto Process

+

.qmd files can be used interactively and they can be used as executables by clicking Render. Clicking the “Render” button starts the rendering process.

+

+
    +
  1. Quarto opens a fresh R session and runs all code from scratch. Quarto calls library(knitr) and “knits” .qmd (Quarto files) into .md (Markdown files).
  2. +
  3. Pandoc converts the .md file into any specified output type.
  4. +
+

Quarto and library(knitr) don’t need to be explicitly loaded and most of the magic happens “under the hood.” Quarto, library(knitr), and Pandoc are all installed with RStudio.2

+

The “Render” workflow has a few advantages:

+
    +
  1. All code is rerun in a clean environment when “Rendering”. This ensures that the code runs in order and is reproducible.
  2. +
  3. It is easier to document code than with inline comments.
  4. +
  5. The output types are really appealing. By creating publishable documents with code, there is no need to copy-and-paste or transpose results.
  6. +
  7. The process is iterable and scalable.
  8. +
-
-

6.4.0.2 construct functions

-
    -
  • construct_shiny()
  • -
  • construct_fact_sheet_html()
  • -
  • construct_fact_sheet_pdf()
  • -
  • construct_slide_show()
  • -
  • construct_web_report()
  • -
-
-

6.4.0.3 use functions

-
    -
  • use_content()
  • -
  • use_css()
  • -
  • use_fact_sheet_html()
  • -
  • use_fact_sheet_pdf()
  • -
  • use_git_ignore_urbn()
  • -
  • use_instructions()
  • -
  • use_iterate()
  • -
  • use_preamble()
  • -
  • use_revealjs()
  • -
  • use_shiny_app()
  • -
  • use_readme_readme()
  • -
  • use_web_report()
  • -
-
-
-

6.4.1 YAML Header

-

Controls the settings of the document.

-
-
-

6.4.2 Markdown

-

Markdown is a lightweight markup language for creating formatted text using a plain-text editor. R Markdown Reference Guide

+
+

6.5 Three Ingredients in a .qmd

+
    +
  1. YAML header
  2. +
  3. Markdown text
  4. +
  5. Code chunks
  6. +
+
+

6.5.1 1. YAML header

+

YAML stands for “yet another markup language.” The YAML header contains meta information about the document including output type, document settings, and parameters that can be passed to the document. The YAML header starts with --- and ends with ---.

+

Here is the simplest YAML header for a PDF document:

+
---
+format: html
+---
+

YAML headers can contain many output specific settings. This YAML header creates an HTML document with code folding and a floating table of contents:

+
---
+format: 
+  html:
+    code-fold: true
+    toc: true
+---  
-
-

6.4.3 Code Chunks

-

Creates the code output for the document.

+
+

6.5.2 2. Markdown text

+

Markdown is a shortcut for Hyper Text Markup Language (HTML). Essentially, simple meta characters corresponding to formatting are added to plain text.

+
Titles and subtitltes
+------------------------------------------------------------
+
+# Title 1
+
+## Title 2
+
+### Title 3
+
+
+Text formatting 
+------------------------------------------------------------
+
+*italic*  
+
+**bold**   
+
+`code`
+
+Lists
+------------------------------------------------------------
+
+- Bulleted list item 1
+- Item 2
+  - Item 2a
+  - Item 2b
+
+1. Item 1
+2. Item 2
+
+Links and images
+------------------------------------------------------------
+
+[text](http://link.com)
+
+![Image caption](images/image.png)
-
-

6.4.4 Create a Markdown HTML Fact Sheet - Piece by Piece

+
+

6.5.3 3. Code chunks

+

+

More frequently, code is added in code chunks:

+
+
```{r}
+2 + 2
+```
+
+
[1] 4
+
+
+

The first argument inline or in a code chunk is the language engine. Most commonly, this will just be a lower case r.

+

Quarto has a rich set of options that go inside of the chunks and control the behavior of Quarto.

+
+
```{r}
+#| eval: false
+
+2 + 2
+```
+
+

In this case, eval makes the code not run. Other chunk-specific settings can be added inside the brackets. Here3 are the most important options:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
OptionEffect
echo: falseHides code in output
eval: falseTurns off evaluation
output: falseHides code output
warning: falseTurns off warnings
message: falseTurns off messages
fig-height: 8Changes figure width in inches4
fig-width: 8Changes figure height in inches
-
-

6.4.5 Step 1: Install library(urbntemplates)

-
install.packages("remotes")
-remotes::install_github("UrbanInstitute/urbntemplates")
-

Step 2: Create a new project using start_project()(Parenthesis should have directory and name of new project). Once you create the project, open a new script and create an html fact sheet using urbntemplates::construct_fact_sheet_html()

-

Step 3: Resave the .Rmd with a better name.

-

Step 4: Knit the R Markdown into a pdf document.

-

Step 5: Add some text into the markdown, some code that appears as code, and some code that shows the result but not the code. Every code chunk should start with {r name} and then set the parameters (T/F, separated by comma. See full factsheet for more parameters.

+
+

6.6 Exercise

+
-
-

6.5 Resources

+
+

6.7 Resources

+
+
+
+
    +
  1. My attempt at making reproducible research more fun.↩︎

  2. +
  3. Rendering to PDF requires a LaTeX distribution. Follow these instructions to install library(tinytex) if you want to make PDF documents.↩︎

  4. +
  5. This table was typed as Markdown code. But sometimes it is easier to use a code chunk to create and print a table. Pipe any data frame into knitr::kable() to create a table that will be formatted in the output of a rendered Quarto document.↩︎

  6. +
  7. The default dimensions for figures change based on the output format. Visit here to learn more.↩︎

  8. +
diff --git a/docs/index.html b/docs/index.html index 6fbb3eb..7de1e59 100644 --- a/docs/index.html +++ b/docs/index.html @@ -7,7 +7,7 @@ - + Intro to R