-
Notifications
You must be signed in to change notification settings - Fork 153
/
getting_sophisticated.Rmd
219 lines (136 loc) · 6.43 KB
/
getting_sophisticated.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
---
title: "Time to get sophisticated"
output:
html_document:
toc: true
toc_float: true
---
There are a lot of ways to get sophisticated here, and we're only covering a few possibilites here. See [RStudio's R Markdown lessons](http://rmarkdown.rstudio.com/lesson-1.html) for other R Markdown capabilities. Plus, R Markdown can render styling from Cascading Style Sheets (CSS) and Hyper Text Markup Language (HTML), which is what non-R Markdown websites use.
So, Googling what you want to do and trying it in your R Markdown files is a good way to learn. Disclaimer: that's how I've learned the following, so there are most definitely better and more elegant ways to do this. But here are some ideas of what you may want to do with your website.
----
## R Markdown options
These are options that you can do from the Front Matter.
<br>
### Table of Contents
Note that on this page:
1. there is a Table of Contents (TOC) there on the left, and
1. it is floating (i.e. it follows you as you scroll down and expands/collapses nicely as you click)
1. This is all done in the front matter:
```
output:
html_document:
toc: true
toc_float: true
```
<!---also note that my numbering in this list renders nicely--->
<br>
### Themes
We can change the website's theme in `_site.yml`. Explore options here: <http://rmarkdown.rstudio.com/html_document_format.html#appearance_and_style>:
>Valid themes include "default", "cerulean", "journal", "flatly", "readable", "spacelab", "united", "cosmo", "lumen", "paper", "sandstone", "simplex", and "yeti".
----
## Simple styling
### images
By default, images are displayed left-aligned.
**Resize** an image using HTML tags (side note: check out [RStudio's R Markdown Cheatsheet](https://www.rstudio.com/resources/cheatsheets/)):
Here's a big image:
`<img src="images/Rmd_cheatsheet.png" width="350px">`
<img src="images/Rmd_cheatsheet.png" width="350px">
And a small image:
`<img src="images/Rmd_cheatsheet.png" width="150px">`
<img src="images/Rmd_cheatsheet.png" width="150px">
**Align** an image using HTML tags:
Small image centered:
`<center><img src="images/Rmd_cheatsheet.png" width="150px"></center>`
<center><img src="images/Rmd_cheatsheet.png" width="150px"></center>
Small image right-aligned:
`<div align="right"><img src="images/Rmd_cheatsheet.png" width="150px"></div>`
<div align="right"><img src="images/Rmd_cheatsheet.png" width="150px"></div>
<br>
### columns
<!---From http://stackoverflow.com/questions/31753897/2-column-section-in-r-markdown:
Put in your css file or directly in rmarkdown--->
<style>
.col2 {
columns: 2 200px; /* number of columns and width in pixels*/
-webkit-columns: 2 200px; /* chrome, safari */
-moz-columns: 2 200px; /* firefox */
}
</style>
<div class="col2">
**Column 1**
- note `<div class="col2">` and `</div>`
- what beautiful bullet points
<br>
<br>
**Column 2**
- I have 3 bullet points here
- which means to align with the left column
- I added a `<br>` there
</div>
<br>
<center>
`<center>**Here the text is centered. Here are good resources:**</center>`
**Here the text is centered. Here are good resources:**
• [w3schools.com - HTML references](http://www.w3schools.com/tags/) • [w3schools.com - CSS tutorial](http://www.w3schools.com/css/) •
</center>
<br>
### colored text
`<font size="3" color="red">Text here!</font>`
<font size="3" color="red">This is red text size 3!</font>
<font size="2" color="blue">This is blue text size 2!</font>
<font face="verdana" color="green">This is green text in verdana!</font>
----
## `R` code
You can execute R code each time you build your website. Even if you don't yet know R, this is a little taste of the types of things you can do. There are two ways you'll see R code; in-line and in a code chunk. This is from the [R Markdown chapter in R for Data Science](http://r4ds.had.co.nz/r-markdown.html).
![](images/r4ds_r-markdown.png)
<br>
### in line
Here is an example inline. The date when I last built the website is `r Sys.Date()`. My favorite number is `r pi + 10`. There are `r length(list.files())` files and folders in our repo. This is not just text; his is executable R code that is `r 10 * 10`% awesome!
<br>
### in a code chunk
And here is an example of a code chunk. Let's plot the first bit of an `iris` variable (which is a dataset that comes with base R).
```{r plot iris}
x <- iris$Sepal.Length[1:15]
plot(x)
```
### calling another file
#### `R` script
You can source an R script. This is a super powerful way to avoid copy-pasting your analyses. For example:
```{r source gdp script, echo=T, warning=FALSE, message=F}
## source an external script
source("advanced/my_script.R")
## use the calc_gdp() function `my_script.R`
my_data <- calc_gdp(my_country <- 'Slovenia')
ggplot(data = my_data, aes(x = year, y = pop)) +
geom_point() +
labs(title = my_data$country)
```
<br>
#### A specific R script: `_site.R`
This is something that R Markdown websites will look for; when it exists it will give you 'global variables' that will be available for all pages across your site.
```{r _site.R, eval=FALSE, warning=FALSE, message=F}
source("_site.R")
```
<br>
#### R Markdown file
You can insert separate R Markdown files too; text and code will be incorporated seamlessly into this page. This R Markdown file is considered the 'parent' file, and the one that is incorporated is the 'child' file. In the code chunk below, we'll say `{r, child='advanced/important_example.Rmd'}`.
```{r child, child='advanced/example.Rmd'}
```
<br>
## Troubleshooting
You'll get errors as you work; this is how you learn. These are a few things you may encounter:
1. If when running `rmarkdown::render_site()` you get this error:
```
Error in object[seq_len(ile)] :
object of type 'symbol' is not subsettable
```
Go to *Session > Restart R* and try again.
2. When knitting, if you get this error:
```
Error in parse_block(g[-1], g[1], params.src) : duplicate label 'setup'
Calls: <Anonymous> ... process_file -> split_file -> lapply -> FUN -> parse_block
Execution halted
```
Here you have 2 `R` code chunks named the same thing (in this case 'setup'), you must change one of them because there cannot be two code chunks with the same name.
3. When you get an error message you don't understand: Copy-paste it into Google.
<center><img src="images/practicalDev_googleErrorMessage.jpg" width="350px"></center>