-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3001e54
commit 342224b
Showing
9 changed files
with
248 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: "Qmd_example" | ||
format: | ||
html | ||
memo-pdf | ||
date: "2024-04-15" | ||
--- | ||
|
||
```{r} | ||
#| label: setup | ||
#| include: false | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
## R Markdown | ||
|
||
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. | ||
|
||
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: | ||
|
||
```{r} | ||
#| label: cars | ||
summary(cars) | ||
``` | ||
|
||
## Including Plots | ||
|
||
You can also embed plots, for example: | ||
|
||
```{r} | ||
#| label: pressure | ||
#| echo: false | ||
plot(pressure) | ||
``` | ||
|
||
Note that the `echo: false` parameter was added to the code chunk to prevent printing of the R code that generated the plot. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
title: "Rmd_example" | ||
output: html_document | ||
date: "2024-04-15" | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
## R Markdown | ||
|
||
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. | ||
|
||
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: | ||
|
||
```{r cars} | ||
summary(cars) | ||
``` | ||
|
||
## Including Plots | ||
|
||
You can also embed plots, for example: | ||
|
||
```{r pressure, echo=FALSE} | ||
plot(pressure) | ||
``` | ||
|
||
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,36 @@ | ||
--- | ||
title: "Week 7: Reactive programming - Shiny" | ||
title: "Week 7: Object-oriented programming and literate programming" | ||
--- | ||
|
||
```{r} | ||
week <- 7 | ||
source(here::here("course_info.R")) | ||
``` | ||
|
||
## Main reference | ||
## Main references | ||
|
||
* [S3](https://adv-r.hadley.nz/s3.html) | ||
* [vctrs](https://vctrs.r-lib.org) | ||
* [roxygen2](https://r-pkgs.org/man.html) | ||
* [Rmarkdown](https://bookdown.org/yihui/rmarkdown/) | ||
* [Quarto](https://quarto.org) | ||
|
||
## What you will learn this week | ||
|
||
* A Q&A on any problems with [Assignment 2](/assignments/A2.qmd) | ||
* More on S3 and vctrs in preparation for [Assignment 4](/assignments/A4.qmd) | ||
* Brief overview of literate programming | ||
|
||
```{r} | ||
#| output: asis | ||
schedule |> | ||
filter(Week == week) |> | ||
mutate(ref = glue::glue("[{Reference}]({Reference_URL})")) |> | ||
pull(ref) | ||
show_slides(week) | ||
``` | ||
|
||
## What you will learn this week | ||
## Other resources | ||
|
||
Shiny | ||
* [Rob's 2020 rstudio::conf talk on Rmarkdown](https://youtu.be/_D-ux3MqGug?si=LeXPIa9K3V9N1rO_) | ||
|
||
```{r} | ||
#| output: asis | ||
show_slides(week) | ||
show_assignments(week) | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
--- | ||
title: ETC4500/ETC5450 Advanced R programming | ||
author: "Week 7: Literate Programming" | ||
format: | ||
beamer: | ||
pdf-engine: pdflatex | ||
aspectratio: 169 | ||
fontsize: "14pt,t" | ||
section-titles: false | ||
knitr: | ||
opts_chunk: | ||
dev: "cairo_pdf" | ||
fig-width: 7.5 | ||
fig-height: 3.5 | ||
include-in-header: ../header.tex | ||
keep-tex: false | ||
--- | ||
|
||
```{r} | ||
#| label: setup | ||
#| include: false | ||
#| cache: false | ||
source(here::here("setup.R")) | ||
source(here::here("course_info.R")) | ||
``` | ||
|
||
## Outline | ||
|
||
\vspace*{0.4cm} | ||
\tableofcontents | ||
|
||
# Literate programming | ||
|
||
## Literate programming | ||
|
||
* Due to Donald Knuth (Stanford), 1984 | ||
* A script or document that contains an explanation of the program logic in a natural language (e.g. English), interspersed with snippets of source code, which can be compiled and rerun. | ||
* Generates two representations\newline from a source file: formatted\newline documentation and "tangled" code. | ||
|
||
\placefig{10.5}{4.6}{width = 6cm}{WEB.png} | ||
|
||
## Literate programming | ||
|
||
* As a programming approach, it never quite caught on. | ||
* But it has become the standard approach for reproducible documents. | ||
|
||
## Literate programming examples | ||
|
||
* WEB (combining Pascal and TeX) | ||
* roxygen2 comments | ||
* technically documentation generation rather than literate programming | ||
* documentation embedded in code, rather than code embedded in documentation | ||
* Sweave documents | ||
* Jupyter notebooks | ||
* Rmarkdown documents | ||
* Quarto documents | ||
|
||
## roxygen2 | ||
|
||
* roxygen2 documentation are just comments to R. | ||
* roxygen2::roxygenize(): | ||
* generates documentation from these comments in the form of Rd files | ||
* adds relevant lines to the NAMESPACE file. | ||
* roxygen2::roxygenize() is called by devtools::document(). | ||
* Advantage: keeps documentation with the code. More readable, less chance for errors. | ||
|
||
## Markdown syntax | ||
\fontsize{14}{16}\sf | ||
Markdown: a "markup" language for formatting text. | ||
|
||
* Headings: | ||
|
||
`# Heading 1`\newline | ||
`## Heading 2` | ||
|
||
* **Bold**: `**bold**`. | ||
* *Italic*: `*italic*`. | ||
* Blockquotes: | ||
|
||
`> blockquote`. | ||
|
||
## Markdown and Rmarkdown | ||
|
||
* Markdown (markup language): | ||
* Extension either `.md` or `.markdown`. | ||
* Used in many places on the web, in note-taking apps, etc. | ||
|
||
* Rmarkdown (markup language): | ||
* an extension of markdown that allows for embedded R code chunks. | ||
* Extension `.Rmd`. | ||
|
||
* Rmarkdown (package): | ||
* an R package that allows for the conversion of `.Rmd` files to other formats. | ||
|
||
## Rmarkdown files | ||
|
||
* Structure: | ||
1. YAML header | ||
2. Markdown content | ||
3. R code chunks surrounded by `` ```{r} `` and `` ``` `` | ||
|
||
* Rmarkdown documents can be compiled to HTML, PDF, Word, and other formats | ||
* Compile with `rmarkdown::render("file.Rmd")` | ||
|
||
## Rmarkdown, knitr and pandoc | ||
|
||
* `rmarkdown::render()` | ||
* Uses `knitr` to run all code chunks, and "knit" the results into a markdown file (replacing R chunks with output). | ||
* Uses `pandoc` to convert the markdown file to the desired output format. | ||
* If PDF output is desired, LaTeX then converts the tex file (from pandoc output) to pdf. | ||
|
||
\centerline{\includegraphics[width = 10cm]{rmd.png}} | ||
|
||
## Some chunk options | ||
\fontsize{14}{16}\sf | ||
|
||
* `eval`: whether to evaluate the code chunk | ||
* `echo`: whether to display the code chunk | ||
* `include`: whether to include the code chunk in the output | ||
* `results = 'hide'` hides printed output. | ||
* `fig.show = 'hide'` hides figures. | ||
* `message`: whether to display messages | ||
* `warning`: whether to display warnings | ||
* `error = TRUE`: continue even if code returns an error. | ||
* `fig.cap`: caption for the figure | ||
* `fig.width`, `fig.height`: width and height of the figure | ||
* `cache`: whether to cache the code chunk | ||
|
||
## Global chunk options | ||
|
||
````{verbatim} | ||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set( | ||
comment = "#>", | ||
collapse = TRUE, | ||
echo = FALSE, | ||
message = FALSE, | ||
warning = FALSE | ||
) | ||
``` | ||
```` | ||
|
||
* The chunk named `setup` will be run before any other chunks. | ||
|
||
## Debugging | ||
|
||
* The Rmarkdown document is compiled in a different environment from your R console. | ||
* If you get an error, try running all chunks (Ctrl+Alt+R). | ||
* If you can't reproduce the error, check the working directory (add `getwd()` in a chunk). | ||
* Try setting `error = TRUE` on problem chunk to help you diagnose what happens. (But change it back!) | ||
* Look at the intermediate files (`.md` or `.tex`) to see what is happening. | ||
|
||
## Quarto | ||
\fontsize{14}{16}\sf | ||
|
||
* Generalization of Rmarkdown (not dependent on R) | ||
* Supports R, Python, Javascript and Julia chunks by using either `knitr`, `jupyter` or `ObservableJS` engines. | ||
* More consistent yaml header and chunk options. | ||
* Many more output formats, and many more options for customizing format. | ||
* Relies on pandoc for converting `md` to other formats. | ||
* Uses pandoc templates (with Lua) for extensions | ||
|
||
\centerline{\includegraphics[width = 10cm]{qmd.png}} |