2016-08-23 05:08:16 +08:00
# R Markdown workflow
2016-08-22 22:31:17 +08:00
2016-08-27 00:39:44 +08:00
Earlier, we discussed a basic workflow for capturing your R code where you work interactively in the _console_, then capture what works in the _script editor_. R Markdown brings together the console and the script editor, blurring the lines between interactive exploration and long-term code capture. You can rapidly iterate within a chunk, editing and re-executing with Cmd/Ctrl + Shift + Enter. When you're happy, you move on and start a new chunk.
2016-08-24 05:40:35 +08:00
2016-08-27 00:39:44 +08:00
R Markdown is also important because it so tightly integrates prose and code. This makes it a great __analysis notebook__ because it lets you develop code and record your thoughts. An analysis notebook shares many of the same goals as a classic lab notebook in the physical sciences. It:
2016-08-24 05:40:35 +08:00
2016-08-27 00:39:44 +08:00
* Records what you did and why you did it. Regardless of how great your
2016-08-24 20:35:22 +08:00
memory is, if you don't record what you do, there will come a time when
2016-08-24 05:40:35 +08:00
you have forgotten important details. Write them down so you don't forget!
2016-08-27 00:39:44 +08:00
* Supports rigorous thinking. You are more likely to come up with a strong
2016-08-24 05:40:35 +08:00
analysis if you record your thoughts as you go, and continue to reflect
2016-08-24 20:35:22 +08:00
on them. This also saves you time when you eventually write up your
2016-08-24 05:40:35 +08:00
analysis to share with others.
2016-08-27 00:39:44 +08:00
* Helps others understand your work. It is rare to do data analysis by
2016-08-24 20:35:22 +08:00
yourself, and you'll often be working as part of a team. A lab notebook
helps you share not only what you've done, but why you did it with your
colleagues or lab mates.
2016-08-24 05:40:35 +08:00
2016-08-27 00:39:44 +08:00
Much of the good advice about using lab notebooks effectively can also be translated to analysis notebooks. I've drawn on my own experiences and Colin Purrington's advice on lab notebooks (<http://colinpurrington.com/tips/lab-notebooks>) to come up with the following tips:
2016-08-24 05:40:35 +08:00
2016-08-24 20:35:22 +08:00
* Ensure each notebook has a descriptive title, an evocative filename, and a
2016-08-24 05:40:35 +08:00
first paragraph that briefly describes the aims of the analysis.
2016-08-24 20:35:22 +08:00
* Use the YAML header date field to record the date you started working on the
2016-08-24 05:40:35 +08:00
notebook:
2016-08-24 20:35:22 +08:00
2016-08-24 05:40:35 +08:00
```yaml
date: 2016-08-23
```
Use ISO8601 YYYY-MM-DD format so that's there no ambiguity. Use it
even if you don't normally write dates that way!
2016-08-24 20:35:22 +08:00
* If you spend a lot of time on an analysis idea and it turns out to be a
dead end, don't delete it! Write up a brief note about why it failed and
leave it in the notebook. That will help you avoid going down the same
dead end when you come back to the analysis in the future.
2016-08-27 00:39:44 +08:00
* Generally, you're better off doing data entry outside of R. But if you
do need to record a small snippet of data, clearly lay it out using
`tibble::tribble()`.
2016-08-24 20:35:22 +08:00
* If you discover an error in a data file, never modify it directly, but
2016-08-24 05:40:35 +08:00
instead write code to correct the value. Explain why you made the fix.
2016-08-24 20:35:22 +08:00
2016-08-27 00:39:44 +08:00
* Before you finish for the day, make sure you can knit the notebook
(if you're using caching, make sure to clear the caches). That will
2016-08-24 20:35:22 +08:00
let you fix any problems while the code is still fresh in your mind.
* If you want your code to be reproducible in the long-run (i.e. so you can
come back to run it next month or next year), you'll need to track the
versions of the packages that your code uses. A rigorous approach is to use
2018-06-20 15:59:31 +08:00
__packrat__, <http://rstudio.github.io/packrat/>, which stores packages
2016-08-27 00:39:44 +08:00
in your project directory, or __checkpoint__,
<https://github.com/RevolutionAnalytics/checkpoint>, which will reinstall
2016-10-03 21:37:33 +08:00
packages available on a specified date. A quick and dirty hack is to include
2018-06-20 15:59:31 +08:00
a chunk that runs `sessionInfo()` --- that won't let you easily recreate
2016-08-27 00:39:44 +08:00
your packages as they are today, but at least you'll know what they were.
2016-08-24 20:35:22 +08:00
* You are going to create many, many, many analysis notebooks over the course
2016-08-24 05:40:35 +08:00
of your career. How are you going to organise them so you can find them
again in the future? I recommend storing them in individual projects,
and coming up with a good naming scheme.