|
| 1 | +<!-- start homework activity --> |
| 2 | + |
| 3 | + |
| 4 | +<div class="notice--warning" markdown="1"> |
| 5 | + |
| 6 | +## <i class="fa fa-pencil-square-o" aria-hidden="true"></i> Homework Submission: Create A Report Using Knitr & RMarkdown |
| 7 | + |
| 8 | +* Create a new rmarkdown `.Rmd` file in `Rstudio`. Name the file: |
| 9 | +`yourLastName-firstInitial-week1.Rmd` example: `wasser-l-week1.Rmd` |
| 10 | + |
| 11 | +* Add an `author:` line to the `YAML` header at the top of your `.Rmd` document. |
| 12 | +* Give your file an appropriate title. Suggestion: `Earth Analytics Spring 2017: Homework - Week 1` |
| 13 | +* At the top of the rmarkdown document (BELOW THE YAML HEADER), add some text |
| 14 | +that describes: |
| 15 | + |
| 16 | + * What an `.Rmd` file is |
| 17 | + * How you can use `knitr` in `R` and what it does. |
| 18 | + * Why using `Rmarkdown` to create reports can be helpful to both you and your colleagues that you work with on a project. |
| 19 | + |
| 20 | +* Create a new CODE CHUNK |
| 21 | +* Copy and paste the code BELOW into the code chunk that you just created. |
| 22 | +* Below the code chunk in your `rmarkdown` document, add some TEXT that describes what the plot that you created |
| 23 | +shows - interpret what you see in the data. |
| 24 | +* Finally, in your own words, summarize what you think the plot shows / tells us about |
| 25 | +the flood and also how the data that produced the plot were likely collected. Use the video |
| 26 | +about the Boulder Floods as a reference when you write this summery. |
| 27 | + |
| 28 | +BONUS: If you know `R`, clean up the plot by adding labels and a title. Or better |
| 29 | +yet, use `ggplot2`! |
| 30 | + |
| 31 | +</div> |
| 32 | + |
| 33 | + |
| 34 | +```r |
| 35 | + |
| 36 | +# load the ggplot2 library for plotting |
| 37 | +library(ggplot2) |
| 38 | + |
| 39 | +# download data from figshare |
| 40 | +# note that we are downloaded the data into your |
| 41 | +download.file(url = "https://ndownloader.figshare.com/files/7010681", |
| 42 | + destfile = "data/boulder-precip.csv") |
| 43 | + |
| 44 | +# import data |
| 45 | +boulder.precip <- read.csv(file="data/boulder-precip.csv") |
| 46 | + |
| 47 | +# view first few rows of the data |
| 48 | +head(boulder.precip) |
| 49 | + |
| 50 | +# when we download the data we create a dataframe |
| 51 | +# view each column of the data frame using it's name (or header) |
| 52 | +boulder.precip$DATE |
| 53 | + |
| 54 | +# view the precip column |
| 55 | +boulder.precip$PRECIP |
| 56 | + |
| 57 | +# q plot stands for quick plot. Let's use it to plot our data |
| 58 | +qplot(x=boulder.precip$DATE, |
| 59 | + y=boulder.precip$PRECIP) |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | +<figure class="half"> |
| 64 | +<a href="/images/rfigs/course-materials/earth-analytics/week-1/intro-knitr-rmd/2016-12-06-Rmd05-knitr/render-plot-1.png"> |
| 65 | +<img src="/images/rfigs/course-materials/earth-analytics/week-1/intro-knitr-rmd/2016-12-06-Rmd05-knitr/render-plot-1.png" alt="example of the plot"> |
| 66 | +</a> |
| 67 | +<figcaption> |
| 68 | +If your code ran properly, the resulting plot should look like this one. |
| 69 | +</figcaption> |
| 70 | +</figure> |
| 71 | + |
| 72 | + |
| 73 | +<figure> |
| 74 | +<a href="/images/course-materials/earth-analytics/week-1/setup-r-rstudio/r-markdown-wk-1.png"> |
| 75 | +<img src="/images/course-materials/earth-analytics/week-1/setup-r-rstudio/r-markdown-wk-1.png" alt="R markdown example image."> |
| 76 | +</a> |
| 77 | +<figcaption> |
| 78 | +Your rmarkdown file should look something like the one above (with your own text |
| 79 | +added to it). Note that the imaglocalhoste above is CROPPED at the bottom. Your rmarkdown |
| 80 | +file will have more code in it. |
| 81 | +</figcaption> |
| 82 | +</figure> |
| 83 | + |
| 84 | +### Troubleshooting: missing plot |
| 85 | + |
| 86 | +If the code above did not produce a plot, please check the following: |
| 87 | + |
| 88 | +#### Check your working directory |
| 89 | + |
| 90 | +If the path to your file is not correct, then the data won't load into `R`. |
| 91 | +If the data don't load into `R`, then you can't work with it or plot it. |
| 92 | + |
| 93 | +To figure out your current working directory use the command: `getwd()` |
| 94 | +Next, go to your finder or file explorer on your computer. Navigate to the path |
| 95 | +that `R` gives you when you type `getwd()` in the console. It will look something |
| 96 | +like the path example: `/Users/your-username/documents/earth-analytics` |
| 97 | + |
| 98 | +```r |
| 99 | +# check your working directory |
| 100 | +getwd() |
| 101 | + |
| 102 | +## [1] "/Users/lewa8222/documents/earth-analytics" |
| 103 | +``` |
| 104 | + |
| 105 | +In the example above, note that my USER directory is called `lewa8222`. Yours |
| 106 | +is called something different. Is there a `data` directory within the earth-analytics |
| 107 | +directory? |
| 108 | + |
| 109 | +<figure> |
| 110 | +<a href="/images/course-materials/earth-analytics/week-1/setup-r-rstudio/data-dir-wk-1.png"> |
| 111 | +<img src="/images/course-materials/earth-analytics/week-1/setup-r-rstudio/data-dir-wk-1.png" alt="data directory example image."> |
| 112 | +</a> |
| 113 | +<figcaption> |
| 114 | +Your working directory should contain a `/data` directory. |
| 115 | +</figcaption> |
| 116 | +</figure> |
| 117 | + |
| 118 | +If not, review the [working directory lesson](/course-materials/earth-analytics/week-1/setup-working-directory/) |
| 119 | +to ensure your working directory is SETUP properly on your computer and in `RStudio`. |
| 120 | + |
| 121 | + |
| 122 | +<!-- end homework activity --> |
0 commit comments