forked from jennybc/happy-git-with-r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage-r-script-and-github.Rmd
124 lines (89 loc) · 5.57 KB
/
usage-r-script-and-github.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Render an R script {#r-test-drive}
An underappreciated fact is that much of what you can do with R Markdown, you can also do with an R script.
If you're in analysis mode and want a report as a side effect, write an R script. If you're writing a report with a lot of R code in it, write Rmd. In either case, render to markdown and/or HTML to communicate with other human beings.
* In R markdown, prose is top-level and code is tucked into chunks.
* In R scripts, code is top-level and prose is tucked into comments. You will use `#'` to request that certain comments appear as top-level prose in the rendered output.
You will continue to specify things like the output format via YAML at the top of the file. This will need to be commented with `#'`.
## Morph R Markdown into a renderable R script
Get yourself a working R Markdown file, such as the one you made in your [Rmd test drive](#rmd-test-drive). Or use the boilerplate Rmd document RStudio makes with *File > New File > R Markdown ...*.
Save the file as `foo.R`, as opposed to `foo.Rmd`. Yes, for a brief moment, you will have R Markdown saved as an R script, but that won't be true for long.
Transform the Rmd to R:
* Anything that's not R code? Like the YAML and the English prose? Protect it with roxygen-style comments: start each line with `#'`.
* Anything that's R code? Let it exist "as is" as top-level code. That means you'll need to change the syntax of R chunk headers like so:
Before: ` ```{r setup, include = FALSE}`
After: `#+ r setup, include = FALSE`
Replace the leading backticks and opening curly brace with `#+`.
Delete the trailing curly brace.
Delete the 3 backticks that end each chunk.
Render the R script through one of these methods:
* Click on the "notebook" icon in RStudio to "Compile Report".
* In RStudio, do *File > Knit Document*.
* In R, do `rmarkdown::render("foo.R")`.
You'll get a markdown and/or HTML report, just as with R Markdown.
If you're having trouble making all the necessary changes and you're frustrated, see below for an example you can copy and paste.
All the workflow tips from the [Rmd test drive](#rmd-test-drive) apply here: when you script an analysis, render it to markdown, commit the `.R`, the `.md`, any associated figures, and push to GitHub. Collaborators can see your code but also browse the result without running it. This makes the current state of your analysis accessible to someone who does not even run R or who wants to take a quick look at things from a cell phone or while on vacation.
## Write a render-ready R script
Instead of morphing an R Markdown file, let's create a render-ready R script directly.
Create a new R script and copy/paste this code into it.
```{r create-temp-dir, include = FALSE}
va_home <- fs::dir_create(fs::file_temp(pattern = "va-example-"))
```
```{r define-demo-code, include = FALSE}
demo_code <- c(
"#' Here's some prose in a very special comment. Let's summarize the built-in",
"#' dataset `VADeaths`.",
"## here is a regular code comment, that will remain as such",
"summary(VADeaths)",
"",
"#' Here's some more prose. I can use usual markdown syntax to make things",
"#' **bold** or *italics*. Let's use an example from the `dotchart()` help to",
"#' make a Cleveland dot plot from the `VADeaths` data. I even bother to name",
"#' this chunk, so the resulting PNG has a decent name.",
"#+ dotchart",
"dotchart(VADeaths, main = \"Death Rates in Virginia - 1940\")"
)
writeLines(demo_code, fs::path(va_home, "render-r-script-demo.R"))
```
```{r eval = FALSE, code = demo_code}
```
Render the R script through one of these methods:
* Click on the "notebook" icon in RStudio to "Compile Report".
* In RStudio, do *File > Knit Document*.
* In R, do `rmarkdown::render("YOURSCRIPT.R")`.
Revel in your attractive looking report with almost zero effort! Seriously, all you had to do was think about when to use special comments `#'` in order to promote that to nicely rendered text.
Drawing on the workflow tips in [Rmd test drive](#rmd-test-drive), let's add some YAML frontmatter, properly commented with `#'`, and request the `github_document`. Here's the whole script again:
```{r augment-demo-code, include = FALSE}
demo_code <- c(
"#' ---",
"#' title: \"R scripts can be rendered!\"",
"#' author: \"Jenny Bryan\"",
"#' date: \"April 1, 2014\"",
"#' output: github_document",
"#' ---",
"#'",
demo_code
)
writeLines(demo_code, fs::path(va_home, "render-r-script-demo.R"))
```
```{r eval = FALSE, code = demo_code}
```
Behind the scenes here we have used `rmarkdown::render()` to render this script and you can go [visit it on GitHub](https://github.com/jennybc/happy-git-with-r/blob/master/render-r-script-demo.md).
```{r render-demo-code, include = FALSE, error = TRUE, eval = !as.logical(Sys.getenv("TRAVIS", unset = "FALSE"))}
## render must happen elsewhere, othewise bookdown yaml clobbers doc yaml
## this is, in fact, why we're working below session temp
withr::with_dir(va_home, rmarkdown::render("render-r-script-demo.R"))
## copy files back into the book
va_files <- fs::dir_ls(va_home, recursive = TRUE)
va_files_rel <- fs::path_rel(va_files, va_home)
md_file <- va_files[fs::path_ext(va_files) == "md"]
fs::file_copy(md_file, fs::path_rel(md_file, va_home), overwrite = TRUE)
fig_dir <- va_files[grepl("_files", fs::path_file(va_files))]
fig_dir_rel <- fs::path_rel(fig_dir, va_home)
if (fs::dir_exists(fig_dir_rel)) {
fs::dir_delete(fig_dir_rel)
}
## TODO: figure out why this directory is deleted in the context of local
## bookdown render
fs::dir_copy(fig_dir, fig_dir_rel)
fs::dir_delete(va_home)
```