Skip to content
Permalink
Browse files

misc fixes

  • Loading branch information...
njtierney committed Nov 12, 2018
1 parent 44f38be commit 452ac91d089e34fdde2fc3d6b4aadf885600562c
Showing with 201 additions and 44 deletions.
  1. +15 −0 common-problems.Rmd
  2. +13 −0 data/oz_gapminder.csv
  3. +7 −0 figures-tables-captions.Rmd
  4. +2 −0 installation.Rmd
  5. +19 −0 references.Rmd
  6. +80 −20 using-rmd.Rmd
  7. +64 −23 why-rmd.Rmd
  8. +1 −1 workflow.Rmd
@@ -2,6 +2,21 @@

There are some things that I run into fairly frequently (and some not so much) when I'm rendering my rmarkdown documents. This section details some the common problems, and the solution that I have found works for me.

To avoid problems in the first place, I try and do the following:

- Develop code in chunks and execute the chunks until they work, then move on.
- knit the document regularly to check for errors.

Then, if there is an error:

- recreate the error in an interactive session:
- restart R
- run all chunks below
- find the chunk that did not work, fix until it does
- run all chunks below
- explore working directory issues
- remember that the rmarkdown directory is where the .Rmd file lives

## "Duplication": Duplicated chunk names

**What it might look like**
@@ -0,0 +1,13 @@
country,continent,year,lifeExp,pop,gdpPercap
Australia,Oceania,1952,69.12,8691212,10039.59564
Australia,Oceania,1957,70.33,9712569,10949.64959
Australia,Oceania,1962,70.93,10794968,12217.22686
Australia,Oceania,1967,71.1,11872264,14526.12465
Australia,Oceania,1972,71.93,13177000,16788.62948
Australia,Oceania,1977,73.49,14074100,18334.19751
Australia,Oceania,1982,74.74,15184200,19477.00928
Australia,Oceania,1987,76.32,16257249,21888.88903
Australia,Oceania,1992,77.56,17481977,23424.76683
Australia,Oceania,1997,78.83,18565243,26997.93657
Australia,Oceania,2002,80.37,19546792,30687.75473
Australia,Oceania,2007,81.235,20434176,34435.36744
@@ -181,3 +181,10 @@ There are many different ways to produce tables in R. We have chosen to show `ka

## Summary


<!-- - `pander` -->
<!-- - `xtable` -->
<!-- - `kableExtra` -->
<!-- - the list goes on! -->
<!-- - https://github.com/ropenscilabs/packagemetrics -->
<!-- - https://ropensci.org/blog/blog/2017/06/27/packagemetrics -->
@@ -46,6 +46,8 @@ install.packages("knitr",
install.packages("tinytex",
dependencies = c("Imports", "Suggests"))
install.packages("here")
install.packages('tidyverse')
```
@@ -3,3 +3,22 @@
[R Markdown: The Definitive Guide](https://bookdown.org/yihui/rmarkdown/)

[Bookdown](https://bookdown.org/yihui/bookdown/)

The RStudio [cheatsheet](https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf)

The [official documentation of knitr](https://yihui.name/knitr/options/)

[R4ds section on rmarkdown](http://r4ds.had.co.nz/r-markdown.html)

Extending word templates: http://rmarkdown.rstudio.com/articles_docx.html

- Templates: https://github.com/Pakillo/rmdTemplates

Notebooks: http://rmarkdown.rstudio.com/r_notebooks.html

- [rmarkdown chapter in R for data science](http://r4ds.had.co.nz/r-markdown.html)
- [knitr in a nutshell](http://kbroman.org/knitr_knutshell/)
- [Maëlle Salmon's Rmarkdown course](https://github.com/maelle/rmd_course_isglobal)
- [Jennifer Thompson's Rmarkdown course](https://github.com/jenniferthompson/RepResearchRMarkdown)

- [happy git with r](http://happygitwithr.com/)
@@ -42,7 +42,7 @@ output: html_document

It starts an ends with three dashes `---`, and had fields in there, such as `title`, `author`, and `output`.

These options in the YAML are actually passed down into the `render` function,
The options in the YAML are actually passed down into the `render` function,

```r
html_document(toc = TRUE, toc_depth = 2, dev = 'svg')
@@ -61,6 +61,52 @@ output:

### Text

Is markdown, as we discussed in the earlier section,

It provides a simple way to mark up text

```
- bullet list
- bullet list
- bullet list
1. numbered list
2. numbered list
3. numbered list
__bold__, **bold**, _italic_, *italic*
> quote of something profound
```

````
```r
# computer code goes in three back ticks
1 + 1
2 + 2
```
````

Would be converted to:


- bullet list
- bullet list
- bullet list

1. numbered list
2. numbered list
3. numbered list

__bold__, **bold**, _italic_, *italic*

> quote of something profound
```{r}
# computer code goes in three back ticks
1 + 1
2 + 2
```


### Code
@@ -69,26 +115,36 @@ We refer to code in an rmarkdown document in two ways, code chunks, and inline c

#### Code chunks

Code chunks are marked by three backticks and curly braces with `r` inside them:

````markdown
`r ''```{r chunk-name}
# a code chunk
```
````

* What is a backtick
* chunk names
* no spaces
* give every chunk a name, it will save you time


* chunk options
* The ones you need to know about:
* fig.size
* fig.width
* out.width
* out.height
* dev
* cache
* eval
* echo
* error
* message
* include
* result

You can control how the code is output by changing the code chunks, which follow the title. The ones that you need to know about right now are:

* cache: TRUE / FALSE. Do you want to save the output of the chunk so it doesn't have to run next time?
* eval: Do you want to evaluate the code?
* echo: Do you want to print the code?
* include: Do you want to include code output in the final output document? Setting to `FALSE` means nothing is put into the output document.

<!-- What are messages, warnings, errors, results? -->

<!-- * error: If you want R to stop on errors, set to `FALSE` -->
<!-- * warning: preserve warnings (produced by `warning()`). IF `FALSE`, all warnings will be printed in the console instead of the output document -->
<!-- * message: Similar to warning, do you want to preserve messages provided by `message()`. -->


<!-- * results: option 'hold' will wait until the code chunk is complete and then -->

You can read more about the options at the official documentation: https://yihui.name/knitr/options/#code-evaluation

#### Inline code

@@ -104,9 +160,8 @@ and `r "\u0060r 'ncol(airquality) \u0060"` variables.
## Creating an rmarkdown document

* rstudio menu system
* what is the template that we have?
* compiling an rmarkdown document
* different compiling formats
* explore the template provided by rstudio
* Compile an rmarkdown document

## Working with an rmarkdown document

@@ -173,3 +228,8 @@ It can be easy to get caught up with how your document looks. I highly recommend
- hist(data$)
- How big is the data?

<!-- # My philosophy on Rmarkdown -->

<!-- - Keep it simple -->
<!-- - focus on content -->
<!-- - Then move it to word or LaTeX if you need to -->
@@ -18,6 +18,10 @@ The goal of this section is to briefly discuss why we want to learn rmarkdown, t
* [Learn how to use markdown](https://www.markdowntutorial.com/)
* Think about why you want to use markdown

## Exercises {.exercise}

1. **Why are we here** Form small groups of 2-4 with your neighbours and discuss how you expect learning rmarkdown might benefit you.

## Reproducibility is a problem

We are in a crisis at the moment where a lot of people cannot reproduce scientific work. This isn't just a few people, and it's not cheap. Estimates say that in the biomedical industry, in the USA, irreproducibility (not being able to reproduce a given piece of work) costs $28 Billion dollars annually [^1]. That's one country, one field, and one year.
@@ -87,10 +91,14 @@ __bold__, **bold**, _italic_, *italic*

With very little marking up, we can create rich text, that actually resembles the text that we want to see.

So, how do we combine this with our R code, into a literate programming environment?
## Exercises {.exercise}

1. **Learn to use markdown** In your small groups, spend five minutes working through this [brief online markdown tutorial](https://www.markdowntutorial.com/)

## Rmarkdown helps complete the solution to the reproducibility problem

So, how do we combine this with our R code, into a literate programming environment?

`Rmarkdown` provides an environment where you can write your complete analysis, and marries your text, and code together into a rich document. You write your code as code chunks, put your text around that, and then hey presto, you have a document you can reproduce.

```{r include=FALSE}
@@ -99,42 +107,80 @@ fit_coef = '`r fit_coef[1]`' # an ugly hack; don't look at me

```yaml
---
title: "Lab Report"
author: "You"
date: "2018-11-12"
output: pdf_document
title: "Exploring gapminder"
author: "Nicholas Tierney"
output:
html_document:
toc: true
toc_depth: 2
---
```

````
```r
`r ''````{r setup , include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
fig.align = "center",
fig.width = 4,
fig.height = 4,
dev = "png",
cache = TRUE)
```
`r ''````{r library}
library(tidyverse)
library(here)
weather <- read_csv(here("data", "weather.csv"))
library(naniar)
library(visdat)
library(broom)
```
`r ''````{r functions}
# A function to scale input to 0-1
scale_01 <- function(x){
(x - min(x, na.rm = TRUE)) / diff(range(x, na.rm = TRUE))
}
```
Let's have a look at the weather
`r ''```{r data-read-in}
data <- read_csv(here::here("data", "oz_gapminder.csv"))
```
# Introduction
let's look at the lifespan
`r ''```r
hist(weather$temp)
`r ''```{r}
hist(data$lifeExp)
```
`r ''```r
fit <- lm(temp ~ ., data = weather)
fit_coef <- coef(fit)
Let's look at the GDP per capita
`r ''```{r example-cache}
hist(data$gdpPercap)
```
The effect of temperature of wind was `r fit_coef[1]`
Let's fit a simple linear model of the effect of year on life expectancy
`r ''```{r example-lm, eval = FALSE}
fit <- lm(lifeExp ~ year, data = data)
fit
```
This can be seen in this table
And let's look at the coefficient table:
```r
knitr::kable(coef(fit))
`r ''```{r coef-table}
fit_coef <- tidy(fit)
knitr::kable(fit_coef,
caption = "A table of the coefficients")
```
The effect of year on life expectancy is `r "\u0060r fit_coef$estimate[2]\u0060"`.
````

<!-- `r fit_coef$estimate[2]`. -->

__Screenshot of this__

So now we got a report. This is great! It has a plot, it has a table, we even refer to some of the values in the text.
@@ -161,8 +207,3 @@ Just updte the data, and render the document again.
That's it.

That is why we use rmarkdown.

## Exercises {.exercise}

1. **Why are we here** Form small groups of 2-4 with your neighbours and discuss how you expect learning rmarkdown might benefit you.
1. **Learn to use markdown** In your small groups, spend five minutes working through this [brief online markdown tutorial](https://www.markdowntutorial.com/)
@@ -124,7 +124,7 @@ If you have an RStudio project file inside the `etc1010` folder, you can instead
health <- read_csv("week1/data/health.csv")
```

## Exercise .{exercise}
## Exercise {.exercise}

* (1-2 minutes) What folders are above the file, "Week1.Rmd"?
* What would be the result of using the following code in week1.Rmd, and then using the code, and then moving this to another location, inside your C drive?

0 comments on commit 452ac91

Please sign in to comment.
You can’t perform that action at this time.